PetGriffin
PetGriffin

Reputation: 545

Problems resampling Pandas DataFrame

just starting out using Pandas and am having trouble resampling a DataFrame. I read the data from EXCEL and print the first 10 lines so

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. However, there are missing days in the later data so I want to fill them with NaN's. 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!

Upvotes: 0

Views: 55

Answers (1)

It_is_Chris
It_is_Chris

Reputation: 14063

asfreq only works when your date column is the index:

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

Upvotes: 1

Related Questions