简体   繁体   中英

How do I pad values with dates as the index using python pandas?

I have the following list with a group of dicts:

results = [{'timestamp': datetime.datetime(2016, 1, 15, 0, 0, tzinfo=<UTC>),
            'value1':100,
            'value2':200}, ... ]

I'm using pandas to pad these results between two utc dates, from_date and to_date with a week of the year frequency. The rest of the values should be 0:

generated_dates = pd.date_range(start=from_date, end=to_date,
                                freq=freq['W'], tz='UTC', normalize=True)

I'm trying to combine the two lists now, in order. So I create two DataFrames so I can do that:

results_df = pd.DataFrame(results)
generated_dates_df = pd.DataFrame(generated_dates, columns=results_df, index=generated_dates)
generated_dates_df.fillna(0, inplace=True)

I then concatenate the data:

pd.concat([results_df, generated_dates_df])

I'm expecting this:

[{'timestamp': datetime.datetime(2015, 1, 1, 0, 0, tzinfo=<UTC>),
  'value1':0,
  'value2':0},
 ...
 {'timestamp': datetime.datetime(2016, 1, 15, 0, 0, tzinfo=<UTC>),
  'value1':100,
  'value2':200},
 {'timestamp': datetime.datetime(2016, 22, , 0, 0, tzinfo=<UTC>),
  'value1':0,
  'value2':0},
 ...,
]

But I keep getting TypeError: data type not understood Is there something I'm missing?

If by "order" you mean sorted by a date index, I think you want join...

ndf = results.join(generated_dates, how="outer")

You might have to use "lsuffix" or "rsuffix".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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