[英]Problems resampling Pandas DataFrame
just starting out using Pandas and am having trouble resampling a DataFrame.刚开始使用 Pandas 并且在重新采样 DataFrame 时遇到了麻烦。 I read the data from EXCEL and print the first 10 lines so
我从 EXCEL 读取数据并打印前 10 行,所以
print df[:5]
gives this给了这个
Date pnew
0 2009-12-23 16:41:00 4.242328
1 2009-12-24 16:41:00 4.248494
2 2009-12-25 16:41:00 4.257310
3 2009-12-26 16:41:00 4.262042
4 2009-12-27 16:41:00 4.264798
which is identical to the data in the XL file.这与 XL 文件中的数据相同。 However, there are missing days in the later data so I want to fill them with NaN's.
但是,后面的数据中缺少天数,所以我想用 NaN 填充它们。 My code looks like this
我的代码看起来像这样
dg = df.asfreq('D')
print(dg)
but now I get this:但现在我明白了:
Date pnew
1970-01-01 NaT NaN
Not really what I was expecting... I think the answer is trivial (if you know it...) but I'm flummoxed.不是我所期待的......我认为答案是微不足道的(如果你知道的话......)但我很困惑。 All suggestions welcome - thanks!
欢迎所有建议 - 谢谢!
asfreq
only works when your date column is the index: asfreq
仅在您的日期列是索引时才有效:
s="""Date,pnew
2009-12-23 16:41:00,4.242328
2009-12-24 16:41:00,4.248494
2009-12-25 16:41:00,4.257310
2009-12-27 16:41:00,4.264798"""
df = pd.read_csv(StringIO(s))
df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date').asfreq('D')
pnew
Date
2009-12-23 16:41:00 4.242328
2009-12-24 16:41:00 4.248494
2009-12-25 16:41:00 4.257310
2009-12-26 16:41:00 NaN
2009-12-27 16:41:00 4.264798
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.