Pandas: Using Unix epoch timestamp as Datetime index

前端 未结 3 1210
误落风尘
误落风尘 2020-12-09 12:06

My application involves dealing with data (contained in a CSV) which is of the following form:

Epoch (number of seconds since Jan 1, 1970), Value
1368431149,         


        
相关标签:
3条回答
  • 2020-12-09 12:34

    Convert them to datetime64[s]:

    np.array([1368431149, 1368431150]).astype('datetime64[s]')
    # array([2013-05-13 07:45:49, 2013-05-13 07:45:50], dtype=datetime64[s])
    
    0 讨论(0)
  • 2020-12-09 12:44

    You can also use Pandas DatetimeIndex like so

    pd.DatetimeIndex(df['timestamp']*10**9)
    

    the *10**9 puts it into the format it's expecting for such timestamps.

    This is nice since it allows you to use functions such as .date() or .tz_localize() on the series.

    0 讨论(0)
  • 2020-12-09 12:50

    You can also use pandas to_datetime:

    df['datetime'] = pd.to_datetime(df["timestamp"], unit='s')
    

    This method requires Pandas 0.18 or later.

    0 讨论(0)
提交回复
热议问题