Round pandas datetime index?

后端 未结 3 467
名媛妹妹
名媛妹妹 2020-12-11 07:58

I am reading multiple spreadsheets of timeseries into a pandas dataFrame and concatenating them together with a common pandas datetime index. The datalogger that logged the

3条回答
  •  离开以前
    2020-12-11 08:38

    Here's a little trick. Datetimes are in nanoseconds (when viewed as np.int64). So round to minutes in nanoseconds.

    In [75]: index = pd.DatetimeIndex([ Timestamp('20120827 12:05:00.002'), Timestamp('20130101 12:05:01'), Timestamp('20130712 15:10:00'), Timestamp('20130712 15:10:00.000004') ])
    
    In [79]: index.values
    Out[79]: 
    array(['2012-08-27T08:05:00.002000000-0400',
           '2013-01-01T07:05:01.000000000-0500',
           '2013-07-12T11:10:00.000000000-0400',
           '2013-07-12T11:10:00.000004000-0400'], dtype='datetime64[ns]')
    
    In [78]: pd.DatetimeIndex(((index.asi8/(1e9*60)).round()*1e9*60).astype(np.int64)).values
    Out[78]: 
    array(['2012-08-27T08:05:00.000000000-0400',
           '2013-01-01T07:05:00.000000000-0500',
           '2013-07-12T11:10:00.000000000-0400',
           '2013-07-12T11:10:00.000000000-0400'], dtype='datetime64[ns]')
    

提交回复
热议问题