Python 使用中的DataFrame.where方法时发生ValueError
我正在编写以下代码,我只想使用Python 使用中的DataFrame.where方法时发生ValueError,python,pandas,datetime,dataframe,where,Python,Pandas,Datetime,Dataframe,Where,我正在编写以下代码,我只想使用pd.where方法获取值的前3分钟,但我得到以下错误: ValueError:数组条件必须与self的形状相同 import pandas as pd import numpy as np index = pd.date_range(start = '2017-06-01 00:00', end='2017-06-01 01:00', freq='1min') values = np.arange(0, len(index)) df = pd.DataFrame
pd.where
方法获取值的前3分钟,但我得到以下错误:
ValueError:数组条件必须与self的形状相同
import pandas as pd
import numpy as np
index = pd.date_range(start = '2017-06-01 00:00', end='2017-06-01 01:00', freq='1min')
values = np.arange(0, len(index))
df = pd.DataFrame(values, index = index)
df.where(df.index <= df.index[0] + pd.DateOffset(minutes=3), np.nan)
将熊猫作为pd导入
将numpy作为np导入
索引=pd.日期\范围(开始='2017-06-01 00:00',结束='2017-06-01 01:00',频率='1分钟')
值=np.arange(0,len(索引))
df=pd.DataFrame(值,索引=index)
df.where(df.index您可以在将df.index
转换为series
后使用df.where
In [557]: df.where(df.index.to_series() <= df.index[0] + pd.DateOffset(minutes=3))
Out[557]:
0
2017-06-01 00:00:00 0.0
2017-06-01 00:01:00 1.0
2017-06-01 00:02:00 2.0
2017-06-01 00:03:00 3.0
2017-06-01 00:04:00 NaN
2017-06-01 00:05:00 NaN
2017-06-01 00:06:00 NaN
... ...
2017-06-01 00:57:00 NaN
2017-06-01 00:58:00 NaN
2017-06-01 00:59:00 NaN
2017-06-01 01:00:00 NaN
[61 rows x 1 columns]
[557]中的:df.where(df.index.to_series()