Converting between datetime, Timestamp and datetime64

前端 未结 12 1451
傲寒
傲寒 2020-11-22 01:43

How do I convert a numpy.datetime64 object to a datetime.datetime (or Timestamp)?

In the following code, I create a datetime,

12条回答
  •  滥情空心
    2020-11-22 02:06

    I think there could be a more consolidated effort in an answer to better explain the relationship between Python's datetime module, numpy's datetime64/timedelta64 and pandas' Timestamp/Timedelta objects.

    The datetime standard library of Python

    The datetime standard library has four main objects

    • time - only time, measured in hours, minutes, seconds and microseconds
    • date - only year, month and day
    • datetime - All components of time and date
    • timedelta - An amount of time with maximum unit of days

    Create these four objects

    >>> import datetime
    >>> datetime.time(hour=4, minute=3, second=10, microsecond=7199)
    datetime.time(4, 3, 10, 7199)
    
    >>> datetime.date(year=2017, month=10, day=24)
    datetime.date(2017, 10, 24)
    
    >>> datetime.datetime(year=2017, month=10, day=24, hour=4, minute=3, second=10, microsecond=7199)
    datetime.datetime(2017, 10, 24, 4, 3, 10, 7199)
    
    >>> datetime.timedelta(days=3, minutes = 55)
    datetime.timedelta(3, 3300)
    
    >>> # add timedelta to datetime
    >>> datetime.timedelta(days=3, minutes = 55) + \
        datetime.datetime(year=2017, month=10, day=24, hour=4, minute=3, second=10, microsecond=7199)
    datetime.datetime(2017, 10, 27, 4, 58, 10, 7199)
    

    NumPy's datetime64 and timedelta64 objects

    NumPy has no separate date and time objects, just a single datetime64 object to represent a single moment in time. The datetime module's datetime object has microsecond precision (one-millionth of a second). NumPy's datetime64 object allows you to set its precision from hours all the way to attoseconds (10 ^ -18). It's constructor is more flexible and can take a variety of inputs.

    Construct NumPy's datetime64 and timedelta64 objects

    Pass an integer with a string for the units. See all units here. It gets converted to that many units after the UNIX epoch: Jan 1, 1970

    >>> np.datetime64(5, 'ns') 
    numpy.datetime64('1970-01-01T00:00:00.000000005')
    
    >>> np.datetime64(1508887504, 's')
    numpy.datetime64('2017-10-24T23:25:04')
    

    You can also use strings as long as they are in ISO 8601 format.

    >>> np.datetime64('2017-10-24')
    numpy.datetime64('2017-10-24')
    

    Timedeltas have a single unit

    >>> np.timedelta64(5, 'D') # 5 days
    >>> np.timedelta64(10, 'h') 10 hours
    

    Can also create them by subtracting two datetime64 objects

    >>> np.datetime64('2017-10-24T05:30:45.67') - np.datetime64('2017-10-22T12:35:40.123')
    numpy.timedelta64(147305547,'ms')
    

    Pandas Timestamp and Timedelta build much more functionality on top of NumPy

    A pandas Timestamp is a moment in time very similar to a datetime but with much more functionality. You can construct them with either pd.Timestamp or pd.to_datetime.

    >>> pd.Timestamp(1239.1238934) #defautls to nanoseconds
    Timestamp('1970-01-01 00:00:00.000001239')
    
    >>> pd.Timestamp(1239.1238934, unit='D') # change units
    Timestamp('1973-05-24 02:58:24.355200')
    
    >>> pd.Timestamp('2017-10-24 05') # partial strings work
    Timestamp('2017-10-24 05:00:00')
    

    pd.to_datetime works very similarly (with a few more options) and can convert a list of strings into Timestamps.

    >>> pd.to_datetime('2017-10-24 05')
    Timestamp('2017-10-24 05:00:00')
    
    >>> pd.to_datetime(['2017-1-1', '2017-1-2'])
    DatetimeIndex(['2017-01-01', '2017-01-02'], dtype='datetime64[ns]', freq=None)
    

    Converting Python datetime to datetime64 and Timestamp

    >>> dt = datetime.datetime(year=2017, month=10, day=24, hour=4, 
                       minute=3, second=10, microsecond=7199)
    >>> np.datetime64(dt)
    numpy.datetime64('2017-10-24T04:03:10.007199')
    
    >>> pd.Timestamp(dt) # or pd.to_datetime(dt)
    Timestamp('2017-10-24 04:03:10.007199')
    

    Converting numpy datetime64 to datetime and Timestamp

    >>> dt64 = np.datetime64('2017-10-24 05:34:20.123456')
    >>> unix_epoch = np.datetime64(0, 's')
    >>> one_second = np.timedelta64(1, 's')
    >>> seconds_since_epoch = (dt64 - unix_epoch) / one_second
    >>> seconds_since_epoch
    1508823260.123456
    
    >>> datetime.datetime.utcfromtimestamp(seconds_since_epoch)
    >>> datetime.datetime(2017, 10, 24, 5, 34, 20, 123456)
    

    Convert to Timestamp

    >>> pd.Timestamp(dt64)
    Timestamp('2017-10-24 05:34:20.123456')
    

    Convert from Timestamp to datetime and datetime64

    This is quite easy as pandas timestamps are very powerful

    >>> ts = pd.Timestamp('2017-10-24 04:24:33.654321')
    
    >>> ts.to_pydatetime()   # Python's datetime
    datetime.datetime(2017, 10, 24, 4, 24, 33, 654321)
    
    >>> ts.to_datetime64()
    numpy.datetime64('2017-10-24T04:24:33.654321000')
    

提交回复
热议问题