英文:
Using pyarrow strings with pandas map or apply
问题
I trying to create a new DataFrame column based on a transformation from another.
我试图基于另一个DataFrame列的转换来创建一个新的列。
I'd like to have the return dtype as pyarrow[string]
and not object
.
我希望返回的数据类型是pyarrow[string]
而不是object
。
But this create an intermediate series with object
dtype.
但是这会创建一个中间的object
数据类型的Series。
Is there a way to directly create a pd.Series with string[pyarrow]
type from s
?
是否有一种方法可以直接从s
创建一个带有string[pyarrow]
类型的pd.Series?
英文:
I trying to create a new DataFrame column based on a transformation from another.
I'd like to have the return dtype as pyarrow[string]
and not object
.
I can do
In [2]: s = pd.Series([1, 2, 1, 2])
In [3]: names = {1: 'Creative', 2: 'VeriFone'}
In [4]: s.map(names)
Out[4]:
0 Creative
1 VeriFone
2 Creative
3 VeriFone
dtype: object
In [5]: s.map(names).astype('string[pyarrow]')
Out[5]:
0 Creative
1 VeriFone
2 Creative
3 VeriFone
dtype: string
But this create an intermediate series with object
dtype.
I also tried:
In [6]: arr = pa.array(['', 'Creative', 'VeriFone'])
In [7]: s.apply(arr.__getitem__)
Out[7]:
0 Creative
1 VeriFone
2 Creative
3 VeriFone
dtype: object
But the return dtype is object
.
Is there a way to directly create a pd.Series with string[pyarrow]
type from s
?
答案1
得分: 0
当调用pd.Series.map
或pd.Series.apply
时,Python代码会被执行,因此无论如何都会有一个中间的Python对象。
如果您正在寻求性能,最好使用矢量化操作。例如,您可以使用join:
s = pd.Series([1, 2, 1, 2])
names = pd.Series({1: 'Creative', 2: 'VeriFone'}, dtype='string[pyarrow]', name='name')
s.to_frame().join(names)['name']
英文:
When calling pd.Series.map
or pd.Series.apply
python code gets executed so there will be an intermediate python object no matter what.
If you're looking for performances it's best to use vectorized operations. For example you can use join:
s = pd.Series([1, 2, 1, 2])
names = pd.Series({1: 'Creative', 2: 'VeriFone'}, dtype='string[pyarrow]', name='name')
s.to_frame().join(names)['name']
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论