convert numerical representation of date (excel format) to python date and time, then split them into two seperate dataframe columns in pandas

后端 未结 1 874
长情又很酷
长情又很酷 2021-01-13 09:41

I\'m importing some data in a spreadsheet. It\'s in a dataframe, but the date is in a numerical representation or format

41516.43

First, I

相关标签:
1条回答
  • 2021-01-13 10:08

    Piecing together from another question:

    In [11]: s = pd.Series([41516.43])
    
    In [12]: from xlrd.xldate import xldate_as_tuple
    
    In [13]: from datetime import datetime
    
    In [14]: s.apply(lambda x: datetime(*xldate_as_tuple(x, 0)))
    Out[14]:
    0   2013-08-30 10:19:12
    dtype: datetime64[ns]
    

    Note: presumably slight difference is due to rounding of the float you gave.

    and the "bare-knuckle no-seat-belts use-at-own-risk" version:

    In [21]: pd.Timestamp('1899-12-30') + (pd.offsets.Day(1).nanos * s).astype(np.timedelta64)
    Out[21]:
    0   2013-08-30 10:19:12
    dtype: datetime64[ns]
    

    I think it's generally preferable to do parse dates while using read_excel.

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