简体   繁体   English

如何使用pandas完成一些缺少日期的时间序列数据

[英]How to complete time series data with some missing dates with pandas

I have dataset with missing dates like this. 我有像这样的缺少日期的数据集。

date,value
2015-01-01,7392
2015-01-03,4928
2015-01-06,8672

This is what I expect to achieve. 这是我期望实现的目标。

date,value
2015-01-01,7392
2015-01-02,7392 # ffill 1st
2015-01-03,4928
2015-01-04,4928 # ffill 3rd
2015-01-05,4928 # ffill 3rd
2015-01-06,8672

I tried a lot, I read the documentation, but I could not find a solutioni. 我尝试了很多,我阅读了文档,但我找不到解决方案。 I guessed using df.resample('d',fill_method='ffill'), but I am not still reaching here. 我猜测使用df.resample('d',fill_method ='ffill'),但我还没到达这里。 Could anyone help me to solve the problem? 谁能帮我解决问题?

This is what I did. 这就是我做的。

>>> import pandas as pd
>>> df = pd.read_csv(text,sep="\t",index_col='date')
>>> df.index = df.index.to_datetime()
>>> index = pd.date_range(df.index[1],df.index.max())

Here I get the DatetimeIndex from 2015-01-01 to 2015-01-06. 这里我从2015-01-01到2015-01-06获得DatetimeIndex。

>>> values = [ x for x in range(len(index)) ]
>>> df2 = pd.DataFrame(values,index=index)

Next I am going to merge the original data and DatetimeIndex. 接下来,我将合并原始数据和DatetimeIndex。

>>> df + df2

             0   value
2015-01-01 NaN NaN
2015-01-02 NaN NaN
2015-01-03 NaN NaN
2015-01-04 NaN NaN
2015-01-05 NaN NaN
2015-01-06 NaN NaN

NaN? 喃? I am puzzled. 我很困惑。

>>> df3 = df + df2
>>> df3.info()

DatetimeIndex: 10 entries, 2015-01-01 to 2015-01-10
Data columns (total 2 columns):
value    0 non-null float64
dtypes: float64(1)

The original value was int, but it converted into float. 原始值为int,但它转换为float。

What is my mistake? 我的错是什么?

Try this: 尝试这个:

import numpy as np
df2 = pd.DataFrame(np.nan, index=index)
df.combine_first(df2).fillna(method='ffill')

combine_first will replace nan values in df2 with values from the original df when they exist. combine_firstdf2 nan值替换为原始df存在的值。 You can then fill the remaining nan values with fillna . 然后,您可以使用fillna填充剩余的nan值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM