如何解决Pandas 字符串下标在 modin 中不起作用以及有关将 Pandas 代码转换为 modin 的相关问题
我最近了解了 modin,并且正在尝试将我的一些代码从 Pandas 转换为 modin。我的理解是 modin 有一些运行速度更快的操作,还有一些没有优化的操作,所以它默认为熊猫。因此,在 Pandas 中运行的任何东西都应该在 modin 中运行,但情况似乎并非如此。
下面的代码是pandas中的WAI,但是modin出现错误:
#import modin.pandas as pd
import pandas as pd
dates = pd.date_range('20180101',periods=6)
pid=pd.Series(list(range(6)))
strings=pd.Series(['asdfjkl;','qwerty','zxcvbnm']*2)
frame={'id':pid,'date':dates,'strings':strings}
df=pd.DataFrame(frame)
x=2
df['first_x_string']=df['strings'].str[0:x]
print(df)
返回:
id date strings first_x_string
0 0 2018-01-01 asdfjkl; as
1 1 2018-01-02 qwerty qw
2 2 2018-01-03 zxcvbnm zx
3 3 2018-01-04 asdfjkl; as
4 4 2018-01-05 qwerty qw
5 5 2018-01-06 zxcvbnm zx
但是当我使用 modin.pandas(交换在开始时注释的行)时,我收到错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-16-e08362b2a4c0> in <module>
1 x=2
----> 2 df['first_x_string']=df['strings'].str[0:x]
3
4 print(df)
TypeError: 'StringMethods' object is not subscriptable
我还收到了其他用户警告,而这些警告是我在 Pandas 中没有收到的:
UserWarning: distributing <class 'list'> object. This may take some time.
UserWarning: distributing <class 'dict'> object. This may take some time.
我的问题是:
我该如何解决这个问题?- 当我希望将代码转换为 modin 时,是否有特定类型的命令可以在 Pandas 中运行但在 modin 中不起作用?
- 用户警告是否表明 modin 中的某些操作比 pandas 慢,因此我应该选择使用它来做什么?
- 此外,使用 modin 执行某些操作(例如 read_csv() 以创建数据帧,然后使用 Pandas 在该数据帧上运行操作,并可能再次使用 modin 来保存数据帧)是否可行(或可取)?对于我当前的流程,加载(以及在较小程度上节省)是最密集的任务。
#========================================
更新:
#========================================
我已经为我提出的特定问题找到了解决方法,但希望回答其他(更一般的)问题。使用计时函数捕获字符串中前 x
个字符的替代方法的代码:
import time
x=2
tic = time.perf_counter()
#df['first_x_string']=df['strings'].str[0:x]
toc = time.perf_counter()
print(f'original completed in {toc-tic:0.4f} seconds')
tic = time.perf_counter()
df['first_x_string']=df['strings'].str.get(0)+df['strings'].str.get(1)
toc = time.perf_counter()
print(f'2x get() completed in {toc-tic:0.4f} seconds')
tic = time.perf_counter()
df['first_x_string']=[y[0:x] for y in df['strings']]
toc = time.perf_counter()
print(f'list comprehension completed in {toc-tic:0.4f} seconds')
print(df)
在一个是示例返回的 100 倍的数据帧上运行这个:
熊猫:
original completed in 0.0016 seconds
2x get() completed in 0.0020 seconds
list comprehension completed in 0.0009 seconds
id date strings first_x_string
0 0 2018-01-01 asdfjkl; as
1 1 2018-01-02 qwerty qw
2 2 2018-01-03 zxcvbnm zx
3 3 2018-01-04 asdfjkl; as
4 4 2018-01-05 qwerty qw
.. ... ... ... ...
595 595 2019-08-19 qwerty qw
596 596 2019-08-20 zxcvbnm zx
597 597 2019-08-21 asdfjkl; as
598 598 2019-08-22 qwerty qw
599 599 2019-08-23 zxcvbnm zx
[600 rows x 4 columns]
modin:
original completed in 0.0000 seconds
2x get() completed in 0.2152 seconds
list comprehension completed in 0.1667 seconds
id date strings first_x_string
0 0 2018-01-01 asdfjkl; as
1 1 2018-01-02 qwerty qw
2 2 2018-01-03 zxcvbnm zx
3 3 2018-01-04 asdfjkl; as
4 4 2018-01-05 qwerty qw
.. ... ... ... ...
595 595 2019-08-19 qwerty qw
596 596 2019-08-20 zxcvbnm zx
597 597 2019-08-21 asdfjkl; as
598 598 2019-08-22 qwerty qw
599 599 2019-08-23 zxcvbnm zx
[600 rows x 4 columns]
这些比较似乎说明 modin 并不总是更快,并重申了我关于何时使用 modin 的问题,以及我们是否可以混合/匹配 Pandas 和 modin(或者这是否不是最佳实践以及原因)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。