我有一个pandas.Series包含整数,但我需要将这些转换为字符串以用于某些下游工具.所以假设我有一个Series对象:
import numpy as np
import pandas as pd
x = pd.Series(np.random.randint(0, 100, 1000000))
Run Code Online (Sandbox Code Playgroud)
在StackOverflow和其他网站上,我见过大多数人认为最好的方法是:
%% timeit
x = x.astype(str)
Run Code Online (Sandbox Code Playgroud)
这大约需要2秒钟.
使用时x = x.apply(str),只需0.2秒.
为什么x.astype(str)这么慢?应该推荐的方式x.apply(str)吗?
我主要对python 3的行为感兴趣.