gpt4 book ai didi

python - 为什么 pandas 对日期时间索引的乘法处理方式不同?

转载 作者:太空宇宙 更新时间:2023-11-04 10:21:18 24 4
gpt4 key购买 nike

Pandas 乘法对列数据框不起作用:

In [1]: import pandas as pd

In [2]: df = pd.DataFrame({'a': [1,2,3]})

In [3]: s = pd.Series([5,6,7])

In [4]: df * s

0 1 2 a
0 NaN NaN NaN NaN
1 NaN NaN NaN NaN
2 NaN NaN NaN NaN

this question 中所述,正确的做法是specify the axis :

In [5]: df.multiply(s, axis='index')

a
0 5
1 12
2 21

但是,对于具有日期时间索引的数据帧,这是不必要的:

In [6]: import numpy as np

In [7]: days = np.arange('2000-12-20', '2000-12-23', dtype='datetime64[D]')

In [8]: df = pd.DataFrame({'a': [1,2,3]}, index=days)

In [9]: s = pd.Series([5,6,7], index=days)

In [10]: df * s

a
2000-12-20 5
2000-12-21 12
2000-12-22 21

这是为什么?为什么第一个示例不起作用,是否可以始终使用 * 而不是 .multiply

最佳答案

看起来这是较早的弃用行为。问题 here .可能最好在这两种情况下都明确。

In [161]: pd.__version__
Out[161]: '0.16.2'

In [160]: df * s
....: FutureWarning: TimeSeries broadcasting along DataFrame index by default is deprecated. Please use DataFrame.<op> to explicitly broadcast arithmetic operations along the index
FutureWarning)
Out[160]:
a
2000-12-20 5
2000-12-21 12
2000-12-22 21

关于python - 为什么 pandas 对日期时间索引的乘法处理方式不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32141759/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com