Converting pandas.tslib.Timestamp to datetime python

后端 未结 9 1119
予麋鹿
予麋鹿 2020-12-05 17:36

I have a df time series. I extracted the indexes and want to convert them each to datetime. How do you go about doing that? I tried to use pa

相关标签:
9条回答
  • 2020-12-05 18:01

    You can convert a Timestamp to a Python datetime object with to_pydatetime(), but it seems that when applied to an entire column that conversion is thwarted:

    >>> ts = pd.tslib.Timestamp.now()
    >>> type(ts)
    <class 'pandas._libs.tslibs.timestamps.Timestamp'>
    >>> type(ts.to_pydatetime())
    <class 'datetime.datetime'>
    >>> df = pd.DataFrame({"now": [datetime.datetime.utcnow()] * 10})
    >>> type(df['now'].iloc[0])
    <class 'pandas._libs.tslibs.timestamps.Timestamp'>
    >>> df['now2'] = df['now'].apply(lambda dt: dt.to_pydatetime())
    >>> type(df['now2'].iloc[0])
    <class 'pandas._libs.tslibs.timestamps.Timestamp'>
    

    Not sure what to make of that. (There are some situations where Pandas' Timestamp object isn't a perfect replacement for Python's datetime object, and you want the real thing.)

    0 讨论(0)
  • 2020-12-05 18:05

    Assuming you are trying to convert pandas timestamp objects, you can just extract the relevant data from the timestamp:

    #Create the data
    data = {1: tslib.Timestamp('2013-01-03 00:00:00', tz=None), 2: tslib.Timestamp('2013-01-04 00:00:00', tz=None), 3: tslib.Timestamp('2013-01-03 00:00:00', tz=None)}
    
    #convert to df
    df = pandas.DataFrame.from_dict(data, orient = 'index')
    df.columns = ['timestamp']
    
    #generate the datetime
    df['datetime'] = df['timestamp'].apply(lambda x: datetime.date(x.year,x.month,x.day))
    

    Of course, if you need seconds, minutes, and hours, you can include those as arguments for the function datetime.datetime as well.

    0 讨论(0)
  • 2020-12-05 18:11

    Just try to_datetime()

    >>> import pandas as pd
    >>> t = pd.tslib.Timestamp('2016-03-03 00:00:00')
    >>> type(t)
    pandas.tslib.Timestamp
    >>> t.to_datetime()  #Warning deprecated!
    datetime.datetime(2016, 3, 3, 0, 0)
    >>> t.to_pydatetime()
    datetime.datetime(2016, 3, 3, 0, 0)
    

    Change to datetime.date type

    >>> t.date()
    datetime.date(2016, 3, 3)
    

    update

    Thanks, @mjp, to_datetime() will be deprecated in the future, use to_pydatetime() instead!

    In [4]: t.to_datetime()
    /Users/qiuwei/Library/Python/2.7/lib/python/site-packages/IPython/core/interactiveshell.py:2881: FutureWarning: to_datetime is deprecated. Use self.to_pydatetime()
      exec(code_obj, self.user_global_ns, self.user_ns)
    Out[4]: datetime.datetime(2016, 3, 3, 0, 0)
    
    0 讨论(0)
提交回复
热议问题