python - Pandas drops timestamp columns in resample -


I have a dataframe, which has two timestamp columns, one of which is to re-sample it as an index Used, the other one remains as the column. If I remind the dataframe again, the balance is left in the form of a column. I want to be sold again as if it is a numerical column (which is actually):

  In the form of import NP import ponds to PD init_time = pd.date_range (start = '2010-01-01 00:00', end = '2010-01-05 00:00', Freq = '12H') valid_time = pd.date_range (start = '2010-01-01 00:00', End = '2010 -01-05 00:00', freq = '12H') data = np.random.random (len (valid_time)) frame = pd.DataFrame (index = valid_time, data = data) frame ['init_time '] = Init_time  

An index in dataframe, 1 numerical column and a timestamp Is

  0 init_time 2010-01-01 00:00:00 0.869667 2010-01 -01 00:00:00 2010-01-01 12:00:00 0.225805 2010-01- 01 12:00:00 2010-01-02 00:00:00 0.348080 2010-01-02 00:00:00 2010 -01-02 12:00:00 0.761399 2010-01-02 12:00:00 2010- 01-03 00:00:00 0.645069 2010-01-03 00:00:00 2010-01-03 12:00: 00 0.133111 2010-01-03 12:00:00 2010-01-04 00:00:00 0.314302 2010-01-04 00:00:00 2010-01-04 12:00:00 0.130491 2010-01-04 12:00: 00010-0 1-05 00:00:00 0.621703 2010-01-05 00: Remember now daily: 00:00  

Remember daily now:

  daily = frame.resample ('D', how = 'mean') 0 01- 01-01 0.547736 2010-01-02 0.554740 2010-01-03 0.389090 2010-01-04 0.222396 2010-01-05 0.621703  

Delete my init_time column I have given it a convergence in a raw integer column first Could it be, but is this easy way?

This is not applicable for datelic (mean), but you can do it like this:

Find numerical results

 in  [48]: numeric = frame.resample ('D', how-to = 'mean')  

Get the Dateal Results Get the first column, which is the Decellix

  in [49]: datelike = frame.loc [:, frame.dtypes.isin ([np.dtype ('datetime64 [Ns] ')])]  

Then see them again; You have to consider them as an integer to calculate them; The timestamp handles this float input (and is essentially the goal of the nearest nanosecond)

 in  [50]: datelike = datelike.resample ('D', how to = lambda x: timestamp (X.view ('ii'). ())  

Put them back together

  in [51]: conat ([numerical, Dietelike], axis = 1) out [51]: 0 init_time 2010-01-01 0.798880 2010-01-01 06:00:00 2010-01-02 0.859781 2010-01-02 06:00:00 2010-01- 03 0.515503 2010- 01-03 06:00:00 2010-01-04 0.505557 2010-01-04 06:00:00 2010-01-05 0.979835 2010-01-05 00:00:00 [5 rows x 2 columns ]  

Comments