Converting pandas.tslib.Timestamp to datetime python

后端 未结 9 1154
予麋鹿
予麋鹿 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: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.

提交回复
热议问题