Reputation: 3919
I have a variable which is a Python object and contains information relating to the date and time that the information was recorded. It is in the following format
Date_time_Created
2012-06-29 11:23:44.882
2012-07-27 14:53:46.909
2014-05-01 16:12:32.005
2014-10-27 18:25:57.403
2014-10-29 10:58:46.013
2014-11-06 22:24:24.872
2014-11-06 22:26:13.218
2015-02-17 04:48:35.229
2015-04-14 07:54:43.969
2015-04-14 07:58:42.896
2015-04-14 08:00:35.552
2015-04-14 08:10:11.627
2015-04-14 08:14:37.842
2015-04-14 08:18:47.501
2015-04-14 08:27:21.895
2015-04-14 08:30:21.376
2015-04-14 08:33:42.146
2015-04-14 08:38:06.271
2015-04-14 08:44:36.056
2015-04-14 08:49:40.434
2015-04-14 08:56:38.785
2015-04-14 08:59:12.542
2015-04-14 09:01:19.538
I would like to be able to extract from this just the date (although knowing how to do the time would also be useful!).
I have tried the following that 'should' work with a string
match = re.search(r'\d{4}-\d{2}-\d{2}', text)
date = datetime.strptime(match.group(), '%Y-%m-%d').date()
however the datatype of the data I am working with is a Python object.
Any ideas?
Thanks in advance!
Upvotes: 0
Views: 2389
Reputation: 619
Isn't it just a datetime object?
If so, you could so something like
dataobject.date # for year, month, day
dataobject.time # for hour, min, sec, mill
https://docs.python.org/2/library/datetime.html
If you are using pandas library, you could try something like this to parse series:
date = pd.to_datetime(data_object, format='%Y-%m-%d').dt.date
http://pandas.pydata.org/pandas-docs/version/0.20/generated/pandas.to_datetime.html
Upvotes: 1
Reputation: 6556
Suppose your data is in your.csv
file:
from datetime import datetime
with open('your.csv') as date_file:
next(date_file)
for line in date_file:
date_object = datetime.strptime(line.strip(),'%Y-%m-%d %H:%M:%S.%f')
print("date:%s, time:%s"%(date_object.date(),date_object.time()))
The output will be:
date:2012-06-29, time:11:23:44.882000
date:2012-07-27, time:14:53:46.909000
date:2014-05-01, time:16:12:32.005000
date:2014-10-27, time:18:25:57.403000
date:2014-10-29, time:10:58:46.013000
date:2014-11-06, time:22:24:24.872000
date:2014-11-06, time:22:26:13.218000
date:2015-02-17, time:04:48:35.229000
date:2015-04-14, time:07:54:43.969000
date:2015-04-14, time:07:58:42.896000
date:2015-04-14, time:08:00:35.552000
date:2015-04-14, time:08:10:11.627000
date:2015-04-14, time:08:14:37.842000
date:2015-04-14, time:08:18:47.501000
date:2015-04-14, time:08:27:21.895000
date:2015-04-14, time:08:30:21.376000
date:2015-04-14, time:08:33:42.146000
date:2015-04-14, time:08:38:06.271000
date:2015-04-14, time:08:44:36.056000
date:2015-04-14, time:08:49:40.434000
date:2015-04-14, time:08:56:38.785000
date:2015-04-14, time:08:59:12.542000
date:2015-04-14, time:09:01:19.538000
Update: pandas version:
import pandas as pd
df = pd.read_csv('your.csv')
print pd.to_datetime(df['Date_time_Created']).dt.date # access the date
print pd.to_datetime(df['Date_time_Created']).dt.time # access the time
Upvotes: 1
Reputation: 865
Method 1 : You could use dateutil library https://dateutil.readthedocs.io/en/stable/
import dateutil.parser
yourdate = dateutil.parser.parse(datestring)
if you want to do it manually here is your code you provided slightly modified: import re,datetime Method 2 :
text ="Date_time_Created 2012-06-29 11:23:44.882 2012-07-27 14:53:46.909 2014-05-01 16:12:32.005 2014-10-27 18:25:57.403 2014-10-29 10:58:46.013 2015-04-14 08:56:38.785 2015-04-14 08:59:12.542 2015-04-14 09:01:19.538"
match = re.findall(r'\d{4}-\d{2}-\d{2}', text)
for i in match:
date = datetime.datetime.strptime(i, '%Y-%m-%d').date()
print(date)
if have replaced re.search with re.finall , so i get a list with all the dates and them i parse it to convert them in to dates. you could handle it as you 'd like.
Upvotes: 0