Convert python date formats - Unusual date formats - Extract% Y% M% D

I have a large dataset with various date data in the following formats:

I am familiar with python time module, strptime () method and strftime () method. However, I'm not sure if these date formats above are called if there is a python module that I can use to convert these unusual date formats.

Any idea how to get the% Y% M% D format from these unusual date formats without writing my own calculator?

Thank.

+2
source share
6

- :

In [1]: import datetime

In [2]: s = '2012265'

In [3]: datetime.datetime.strptime(s, '%Y%j')
Out[3]: datetime.datetime(2012, 9, 21, 0, 0)

In [4]: d = '41213'

In [5]: datetime.date(1900, 1, 1) + datetime.timedelta(int(d))
Out[5]: datetime.date(2012, 11, 2)

, %j , ( , %Y). - 1 1900 .

- , , , .

+7

. , , , - ?

import datetime

def days_since_jan_1_1900_to_datetime(d):
    return datetime.datetime(1900,1,1) + \
        datetime.timedelta(days=d)

, , (, 3 , 100, , 2 1 - , 4 ?), , .

+2

Python:

, Excel ( 1 1900 , - 1 1904 ); . https://support.microsoft.com/en-us/help/214330/differences-between-the-1900-and-the-1904-date-system-in-excel .

, . , 1900 1 1900 1 ( 0).

import datetime

EXCEL_DATE_SYSTEM_PC=1900
EXCEL_DATE_SYSTEM_MAC=1904

i = 42129  # Excel number for 5-May-2015
d = datetime.date(EXCEL_DATE_SYSTEM_PC, 1, 1) + datetime.timedelta(i-2)
+1

According to http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior , the day of the year is "% j", while the first case can be resolved toordinal () and fromordinal ():date.fromordinal(date(1900, 1, 1).toordinal() + x)

0
source

I think timedelta.

import datetime
d = datetime.timedelta(days=41213)
start = datetime.datetime(year=1900, month=1, day=1)
the_date = start + d

For the second, you can 2012265[:4]get the year and use the same method.

edit: see answer s %jfor the second.

0
source
from datetime import datetime 

df(['timeelapsed'])=(pd.to_datetime(df['timeelapsed'], format='%H:%M:%S') - datetime(1900, 1, 1)).dt.total_seconds()
0
source

All Articles