Harshit Singh
Harshit Singh

Reputation: 635

Conversion of set of numbers into Date Format using Python

I have a dataframe named 'train' with column ID which represents 'date' in a very unusual manner. For e.g. certain entry in ID:

For example, the value of ID 2013043002 represents the date 30/04/2013 
02:00:00

First 4 digits represents year, subsequent 2 digits represent month and day respectively. And last two digits represent time.

So I want to convert this into proper date time format to perform time series analysis.

Upvotes: 1

Views: 1006

Answers (4)

Örlog
Örlog

Reputation: 37

By using the module datetime you can do that easily with the function strptime :

my_date = datetime.datetime.strptime(ID, "%Y%m%d%H")

"%Y%m%d%H" is the format of your date : %Y is the year, %m is the month(0 padded), %d is the day(0 padded) and %H is the hour(24H, 0 padded). See http://strftime.org/ for more.

Upvotes: 0

Nestor Colt
Nestor Colt

Reputation: 377

First, if you are gonna have ALWAYS the same input style in the Id you could play with string or digit formating ...

Id = 2013043002 
Year = Id[0:3] 
Month = Id[4:5] 
Day = Id[6:7] 
Time= Id[-2:-1]  

DateFormat = "{}-{}-{}".format(Day,Month,Year)
TimeFormar = "%d:00:00"%Time 
Print (DateFormat) 
Output:
04:30:2013 

Then with this you could wrap it into a function and pass every Ids by loops and manage your data.

Of course, if you dont know your previous ID incomming format you should used the other time module options, and manage the string formating to show it in the order you want.

Upvotes: 0

jezrael
jezrael

Reputation: 862911

Use to_datetime with parameter format - check http://strftime.org/:

df = pd.DataFrame({'ID':[2013043002,2013043002]})

df['ID'] = pd.to_datetime(df['ID'], format='%Y%m%d%H')
print(df)
                   ID
0 2013-04-30 02:00:00
1 2013-04-30 02:00:00

print(df['ID'].dtype)
datetime64[ns]

Upvotes: 3

Arunesh Singh
Arunesh Singh

Reputation: 3535

Use datetime for date time manipulations.

datetime.strptime(d,"%Y%m%d%H").strftime("%d/%m/%Y %H:%M:%S")

Upvotes: 2

Related Questions