1

I would like to fill in missing values in my pandas dataframe. Optimally I would like the minute column to range from 0-60 for each hour. Unfortunately, the data generating process did not record any rows where sub_count = 0. Is there anyway to do this? My data covers the dates 2014-03-31 and 2014-04-01.

df = 

   sub_count        date  hour  minute
0          1  2014-03-31     0       0
1          1  2014-03-31     0       4
2          1  2014-03-31     0       5
3          1  2014-03-31     0       6
4          2  2014-03-31     0       7
...

1 Answer 1

3

Construct a DatetimeIndex (you may be able to do this while reading the data in, depending on how it's stored):

df = df.set_index(pd.to_datetime(df.date + 'T' +
                                 df.hour.astype(str) + ':' +
                                 df.minute.astype(str))

In [23]: df = df['sub_count']

In [24]: df
Out[24]: 
2014-03-31 00:00:00    1
2014-03-31 00:04:00    1
2014-03-31 00:05:00    1
2014-03-31 00:06:00    1
2014-03-31 00:07:00    2
Name: sub_count, dtype: int64

Then resample:

In [26]: df.resample('T')
Out[26]: 
2014-03-31 00:00:00     1
2014-03-31 00:01:00   NaN
2014-03-31 00:02:00   NaN
2014-03-31 00:03:00   NaN
2014-03-31 00:04:00     1
2014-03-31 00:05:00     1
2014-03-31 00:06:00     1
2014-03-31 00:07:00     2
Freq: T, Name: sub_count, dtype: float64
Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting an: unsupported operand type(s) for +: 'datetime.date' and 'str' error. If I add .astype(str) to the end of df.date, the code runs but df.resample('T') doesn't seem to do anything :(
what kind of dataframe is this? I can't edit the column names anymore.
Do you have other data columns than sub_count? df = df['sub_count'] selected the only data column in your example, so it's a Series now. You can keep it as a DataFrame with df = df[['sub_count']]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.