Reputation: 1677
I have this following timestamp:
TimeStamp="20160820T210239+0100"
I would like to extract the year, month, day and hour from this format.
I'm working with pandas. Is there any way to parse this timestamp to the format I mentioned?
Kind regards
Upvotes: 2
Views: 15522
Reputation: 3752
You should be able to use to_datetime
function from pandas
. In your case it should automatically pick up the date info, given the format:
import pandas
df = pd.DataFrame([['20160820T210239+0100']], columns=['ts'])
df['date'] = pd.to_datetime(df['ts']) ## pandas recognizes your format
df['day'] = df['date'].dt.day
df['month'] = df['date'].dt.month
df['year'] = df['date'].dt.year
df
Out[1]:
ts date day month year
0 20160820T210239+0100 2016-08-20 21:02:39+01:00 20 8 2016
I am assuming you need this in pandas, given the tag of the question. If you need just in pure python
, you can use datetime
:
import datetime
dt = datetime.datetime.strptime('20160820T210239+0100', '%Y%m%dT%H%M%S%z')
print (dt.year, dt.month, dt.day)
Out[2]:
2016 8 20
Upvotes: 5