ValueError: The truth value of a Series is ambiguous. Pandas
ValueError: The truth value of a Series is ambiguous. Pandas
我使用了以下代码:
data1['Close'].shift(freq='1d').pct_change(data1['Low'])
由于某种原因它正在返回:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
能否请您告知为什么会出现该错误?
我曾将它用于如下所示的数据:
High Low Open Close Volume \
Date
2018-01-02 10495.200195 10404.650391 10477.549805 10442.200195 153400.0
2018-01-03 10503.599609 10429.549805 10482.650391 10443.200195 167300.0
2018-01-04 10513.000000 10441.450195 10469.400391 10504.799805 174900.0
2018-01-05 10566.099609 10520.099609 10534.250000 10558.849609 180900.0
2018-01-08 10631.200195 10588.549805 10591.700195 10623.599609 169000.0
... ... ... ... ...
2020-04-15 9261.200195 8874.099609 9196.400391 8925.299805 879100.0
2020-04-16 9053.750000 8821.900391 8851.250000 8992.799805 719400.0
2020-04-17 9324.000000 9091.349609 9323.450195 9266.750000 684200.0
2020-04-20 9390.849609 9230.799805 9390.200195 9261.849609 726400.0
2020-04-21 9044.400391 8909.400391 9016.950195 8942.099609 0.0
我相信您需要 shift
:
计算的百分比变化
data1['pct'] = 1 - data1['Low'].div(data1['Close'].shift(freq='1d'))
print (data1)
High Low Open Close Volume \
Date
2018-01-02 10495.200195 10404.650391 10477.549805 10442.200195 153400.0
2018-01-03 10503.599609 10429.549805 10482.650391 10443.200195 167300.0
2018-01-04 10513.000000 10441.450195 10469.400391 10504.799805 174900.0
2018-01-05 10566.099609 10520.099609 10534.250000 10558.849609 180900.0
2018-01-08 10631.200195 10588.549805 10591.700195 10623.599609 169000.0
2020-04-15 9261.200195 8874.099609 9196.400391 8925.299805 879100.0
2020-04-16 9053.750000 8821.900391 8851.250000 8992.799805 719400.0
2020-04-17 9324.000000 9091.349609 9323.450195 9266.750000 684200.0
2020-04-20 9390.849609 9230.799805 9390.200195 9261.849609 726400.0
2020-04-21 9044.400391 8909.400391 9016.950195 8942.099609 0.0
pct
Date
2018-01-02 NaN
2018-01-03 0.001211
2018-01-04 0.000168
2018-01-05 -0.001456
2018-01-08 NaN
2020-04-15 NaN
2020-04-16 0.011585
2020-04-17 -0.010959
2020-04-20 NaN
2020-04-21 0.038054
或者:
s = data1['Close'].shift(freq='1d')
data1['pct'] = data1['Low'].sub(s).div(s)
print (data1)
High Low Open Close Volume \
Date
2018-01-02 10495.200195 10404.650391 10477.549805 10442.200195 153400.0
2018-01-03 10503.599609 10429.549805 10482.650391 10443.200195 167300.0
2018-01-04 10513.000000 10441.450195 10469.400391 10504.799805 174900.0
2018-01-05 10566.099609 10520.099609 10534.250000 10558.849609 180900.0
2018-01-08 10631.200195 10588.549805 10591.700195 10623.599609 169000.0
2020-04-15 9261.200195 8874.099609 9196.400391 8925.299805 879100.0
2020-04-16 9053.750000 8821.900391 8851.250000 8992.799805 719400.0
2020-04-17 9324.000000 9091.349609 9323.450195 9266.750000 684200.0
2020-04-20 9390.849609 9230.799805 9390.200195 9261.849609 726400.0
2020-04-21 9044.400391 8909.400391 9016.950195 8942.099609 0.0
pct
Date
2018-01-02 NaN
2018-01-03 -0.001211
2018-01-04 -0.000168
2018-01-05 0.001456
2018-01-08 NaN
2020-04-15 NaN
2020-04-16 -0.011585
2020-04-17 0.010959
2020-04-20 NaN
2020-04-21 -0.038054
我使用了以下代码:
data1['Close'].shift(freq='1d').pct_change(data1['Low'])
由于某种原因它正在返回:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
能否请您告知为什么会出现该错误?
我曾将它用于如下所示的数据:
High Low Open Close Volume \
Date
2018-01-02 10495.200195 10404.650391 10477.549805 10442.200195 153400.0
2018-01-03 10503.599609 10429.549805 10482.650391 10443.200195 167300.0
2018-01-04 10513.000000 10441.450195 10469.400391 10504.799805 174900.0
2018-01-05 10566.099609 10520.099609 10534.250000 10558.849609 180900.0
2018-01-08 10631.200195 10588.549805 10591.700195 10623.599609 169000.0
... ... ... ... ...
2020-04-15 9261.200195 8874.099609 9196.400391 8925.299805 879100.0
2020-04-16 9053.750000 8821.900391 8851.250000 8992.799805 719400.0
2020-04-17 9324.000000 9091.349609 9323.450195 9266.750000 684200.0
2020-04-20 9390.849609 9230.799805 9390.200195 9261.849609 726400.0
2020-04-21 9044.400391 8909.400391 9016.950195 8942.099609 0.0
我相信您需要 shift
:
data1['pct'] = 1 - data1['Low'].div(data1['Close'].shift(freq='1d'))
print (data1)
High Low Open Close Volume \
Date
2018-01-02 10495.200195 10404.650391 10477.549805 10442.200195 153400.0
2018-01-03 10503.599609 10429.549805 10482.650391 10443.200195 167300.0
2018-01-04 10513.000000 10441.450195 10469.400391 10504.799805 174900.0
2018-01-05 10566.099609 10520.099609 10534.250000 10558.849609 180900.0
2018-01-08 10631.200195 10588.549805 10591.700195 10623.599609 169000.0
2020-04-15 9261.200195 8874.099609 9196.400391 8925.299805 879100.0
2020-04-16 9053.750000 8821.900391 8851.250000 8992.799805 719400.0
2020-04-17 9324.000000 9091.349609 9323.450195 9266.750000 684200.0
2020-04-20 9390.849609 9230.799805 9390.200195 9261.849609 726400.0
2020-04-21 9044.400391 8909.400391 9016.950195 8942.099609 0.0
pct
Date
2018-01-02 NaN
2018-01-03 0.001211
2018-01-04 0.000168
2018-01-05 -0.001456
2018-01-08 NaN
2020-04-15 NaN
2020-04-16 0.011585
2020-04-17 -0.010959
2020-04-20 NaN
2020-04-21 0.038054
或者:
s = data1['Close'].shift(freq='1d')
data1['pct'] = data1['Low'].sub(s).div(s)
print (data1)
High Low Open Close Volume \
Date
2018-01-02 10495.200195 10404.650391 10477.549805 10442.200195 153400.0
2018-01-03 10503.599609 10429.549805 10482.650391 10443.200195 167300.0
2018-01-04 10513.000000 10441.450195 10469.400391 10504.799805 174900.0
2018-01-05 10566.099609 10520.099609 10534.250000 10558.849609 180900.0
2018-01-08 10631.200195 10588.549805 10591.700195 10623.599609 169000.0
2020-04-15 9261.200195 8874.099609 9196.400391 8925.299805 879100.0
2020-04-16 9053.750000 8821.900391 8851.250000 8992.799805 719400.0
2020-04-17 9324.000000 9091.349609 9323.450195 9266.750000 684200.0
2020-04-20 9390.849609 9230.799805 9390.200195 9261.849609 726400.0
2020-04-21 9044.400391 8909.400391 9016.950195 8942.099609 0.0
pct
Date
2018-01-02 NaN
2018-01-03 -0.001211
2018-01-04 -0.000168
2018-01-05 0.001456
2018-01-08 NaN
2020-04-15 NaN
2020-04-16 -0.011585
2020-04-17 0.010959
2020-04-20 NaN
2020-04-21 -0.038054