Converting a number to a custom datetime

Date1: 20061201
Date2: 01/12/2006

How can I use pandas in Python to convert date1 to date2 (day / month / year) format? Thank! Date1 and Date2 are two columns in csv files.

+3


source to share


5 answers


Data:

In [151]: df
Out[151]:
       Date
0  20061201
1  20170530

      

Option 1:



In [152]: pd.to_datetime(df.Date, format='%Y%m%d').dt.strftime('%d/%m/%Y')
Out[152]:
0    01/12/2006
1    30/05/2017
Name: Date, dtype: object

      

Option 2:

In [153]: df.Date.astype(str).str.replace('(\d{4})(\d{2})(\d{2})', r'\3/\2/\1')
Out[153]:
0    01/12/2006
1    30/05/2017
Name: Date, dtype: object

      

+2


source


If you use pandas

and want to return an objecttimestamp

pd.to_datetime('20061201')

Timestamp('2006-12-01 00:00:00')

      

If you need a line back

str(pd.to_datetime('20061201').date())

'2006-12-01'

      


Assuming you have a dataframe df



df = pd.DataFrame(dict(Date1=['20161201']))

      

Then you can use the same techniques in vector form.

as timestamps

df.assign(Date2=pd.to_datetime(df.Date1))

      Date1       Date2
0  20161201  2016-12-01

      

like strings

df.assign(Date2=pd.to_datetime(df.Date1).dt.date.astype(str))

      Date1       Date2
0  20161201  2016-12-01

      

+1


source


The easiest way is probably to use the date parsing provided by datetime :

from datetime import datetime
datetime.strptime(str(20061201), "%Y%m%d")

      

You can apply this transformation to all strings in your pandas framework / series using the following:

from datetime import datetime
def convert_date(d):
    return datetime.strptime(str(d), "%Y%m%d")
df['Date2'] = df.Date1.apply(convert_date)

      

This will add a column Date2

to your dataframe df

which is a datetime representation of the column Date1

.

Then you can serialize the date with strftime

:

def serialize_date(d):
    return d.strftime(d, "%d/%m/%Y")
df['Date2'] = df.Date2.apply(serialize_date)

      

Alternatively, you can do the whole thing with string manipulation:

def reformat_date(d):
    year = d // 10000
    month = d % 10000 // 100
    day = d % 100
    return "{day}/{month}/{year}".format(day=day, month=month, year=year)
df['Date2'] = df.Date1.apply(reformat_date)

      

This is slightly faster than using the parser provided strptime

.

0


source


import datetime
A=datetime.datetime.strptime('20061201','%Y%m%d')
A.strftime('%m/%d/%Y')

      

0


source


You can use the apply function and the lambda here.

Suppose you have a dataset named df

as shown below:

id    date1
0     20061201
2     20061202

      

You can use the code as below:

df['date2'] = df['date1'].apply(lambda x: x[6:] + '/' + x[4:6] + '/' + x[:4])

      

The result will be:

id      date1      date2
 0     20061201    01/12/2016
 2     20061202    02/12/2016

      

0


source







All Articles