相关疑难解决方法(0)

将一系列int转换为字符串 - 为什么应用比astype快得多?

我有一个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的行为感兴趣.

python string performance python-internals pandas

26
推荐指数
2
解决办法
1490
查看次数

标签 统计

pandas ×1

performance ×1

python ×1

python-internals ×1

string ×1