How to modify Datetime index format (UTC) in Pandas?

前端 未结 1 962

I have a df that looks like this:

2015-01-29 08:30:00-05:00  199425  199950  199375  199825                  
2015-01-29 08:45:00-05:00  199825  199850  199650           


        
相关标签:
1条回答
  • This is an old question from Jan 2015. But since there is no answer yet (although lots of comments), here is an answer in Oct 2019. The original questioner probably found an answer already but just as a reference for the future.

    https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html

    https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

    import pandas as pd
    
    # create dataframe
    df = pd.DataFrame({
        'date_original': ['2015-01-29 08:30:00-05:00', '2015-01-29 08:45:00-05:00', '2015-01-29 09:00:00-05:00'],
        'measurement': [199425, 199825, 199825]
    })
    
    # make sure to convert date column to datetime, not string
    df['date_original'] = pd.to_datetime(df['date_original'])
    
    print('Original dataframe:')
    print(df)
    print()
    
    # remove the suffix from the date
    df['date_transform'] = pd.to_datetime(df['date_original']).dt.strftime('%Y-%m-%d %H:%M:%S')
    
    print('Transformed dataframe:')
    print(df)
    print()
    df
    
    0 讨论(0)
提交回复
热议问题