pandas datetime to unix timestamp seconds

后端 未结 2 1072
清歌不尽
清歌不尽 2020-12-08 16:28

From the official documentation of pandas.to_datetime we can say,

unit : string, default ‘ns’

unit of the arg (D,s,ms,us,ns)

相关标签:
2条回答
  • 2020-12-08 16:49

    I think you misunderstood what the argument is for. The purpose of origin='unix' is to convert an integer timestamp to datetime, not the other way.

    pd.to_datetime(1.547559e+09, unit='s', origin='unix') 
    # Timestamp('2019-01-15 13:30:00')
    

    Conversely, you can get the timestamp by converting to integer (to get nanoseconds) and divide by 109.

    pd.to_datetime(['2019-01-15 13:30:00']).astype(int) / 10**9
    # Float64Index([1547559000.0], dtype='float64')
    

    Update

    Pandas docs recommend using the following method:

    # create test data
    dates = pd.to_datetime(['2019-01-15 13:30:00'])
    
    # calculate unix datetime
    (dates - pd.Timestamp("1970-01-01")) // pd.Timedelta('1s')
    
    [out]:
    Int64Index([1547559000], dtype='int64')
    

    Not as fast as the method shown above, but this makes no assumption about how pandas internally stores its datetime objects.

    0 讨论(0)
  • 2020-12-08 16:52

    In case you are accessing a particular datetime64 object from the dataframe, chances are that pandas will return a Timestamp object which is essentially how pandas stores datetime64 objects.

    You can use pd.Timestamp.to_datetime64() method of the pd.Timestamp object to convert it to numpy.datetime64 object with ns precision.

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