to_datetime vs to_pydatetime trying to convert str to datetime - python

I have a DataFrame with one columns that is a date and a time and is a string.
The format of the date and time is like this: 4/27/2021 12:39
This is what I have so far to try and convert the string into a datetime:
new_list = []
for i in range(len(open_times)):
date = df.iloc[i]['Open Datetime']
good_date = date.to_datetime()
# good_date = date.topydatetime()
new_list.append(good_date)
I have used to_pydatetime() in the past however the string was in a different format.
When I run the code from above I get this error: AttributeError: 'str' object has no attribute 'to_datetime' and I get the same error when I run the commented out line except with to_pydatetime.
Any thoughts on how to resolve this error? I think that this is happening because the format of the string is different than it typically is.

You need to use datetime.strptime(date_string, format) to convert a string to datetime type
from datetime import datetime
for i in range(len(open_times)):
date = df.iloc[i]['Open Datetime']
good_date = datetime.strptime(date, '%m/%d/%Y %H:%M')
But you could use pd.to_datetime directly
df['Open Datetime'] = pd.to_datetime(df['Open Datetime'])
# Convert a column to list
new_list = df['Open Datetime'].values.tolist()

Related

How to convert the string to date time format?

I have the following string 20211208_104755, representing date_time format. I want to convert it to the python datetime format using datetime.strip() method.
mydatetime = "20211208_104755"
datetime_object = datetime.strptime(mydatetime, '%y/%m/%d')
However I am getting the following error.
ValueError: time data '20211208' does not match format '%y/%m/%d'
The second argument in strptime has to match the pattern of your datetime string. You can find the patterns and their meaning on https://docs.python.org/3/library/datetime.html
In your case, you can format it as
from datetime import datetime
mydatetime = "20211208_104755"
datetime_object = datetime.strptime(mydatetime, '%Y%m%d_%H%M%S')
print(datetime_object)
>>> 2021-12-08 10:47:55
what should work is to define how the mydatetime string is composed.
example:
%Y is the year (4 digits); check here for format (section strftime() Date Format Codes)
So in your example I would assume it's like this:
mydatetime = "20211208_104755"
datetime_object = datetime.strptime(mydatetime, '%Y%m%d_%H%M%S')
print (datetime_object)
result
2021-12-08 10:47:55
and
type(datetime_object)
datetime.datetime

Date Format conversion in pyspark

I have a string column in df which contains date in dd/MM/yyyy format and I want to convert that format to yyyy-MM-dd using with column
If you know you will have a consistent format in your column, you can pass this to 'to_datetime'.
You can try like below-
df['column_name'] = pd.to_datetime(df['column_name'], format='%d/%m/%y').dt.strftime('%Y-%m-%d')
Using python's datetime lib it can be done accordingly:
from datetime import datetime
date_string = '10/10/2000'
datetime_object = datetime.strptime(date_string, '%d/%m/%Y')
converted_date_string = datetime_object.strftime('%Y-%m-%d')

Convert Timestamp to ISO Date

I am extracting Data from Mongodb using some date filter. In mongo my date is in ISO format . As i am dynamically adding date from some variable which is in timestamp format(2019-07-15 14:54:53).Getting Empty Result
curs = col1.aggregate([{'$match':{update_col: {'$gte': last_updt }}},{'$project':json_acceptable_string}])
I am expecting Rows after filtering but acual its giving empty dataset
you can use datetime.strptime to parse the original string to a datetime object, then use datetime.isoformat to get it in ISO format.
try this:
import datetime
original_date = '2019-07-15 14:54:53'
date_obj = datetime.datetime.strptime(original_date, "%Y-%m-%d %H:%M:%S")
iso_date = date_obj.isoformat()
print(iso_date)
try this
from dateutil import parser as date_parser
dt_obj = date_parser.parse('2019-07-15 14:54:53')
where dt_obj is an object of standard datetime.datetime class
You can use fromisoformat.
Try
from datetime import datetime
iso_string = '2019-07-15 14:54:53'
you_date_obj = datetime.fromisoformat(iso_string)

Is there a way to extract time from datetime objects in Python?

I want to extract time values from a datetime object in Python. This is the code I used:
t = '2018-12-16 17:59:00'
t.strftime('%H:%M:%S')
There is clearly something wrong with the code because I am getting this error:
AttributeError: 'str' object has no attribute 'strftime'
I am using Python 3 and I need to convert around 30000 datetime values.
from datetime import datetime as dt
t = '2018-12-16 17:59:00'
t = dt.strptime(t, '%Y-%m-%d %H:%M:%S')
print(t.strftime('%H:%M:%S'))
in datetime methods
strptime is the mehtod to convert from string to datetime
strftime is the method to convert from datetime to string
That's a string, not a datetime object. You should probably be using a datetime object:
t = datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])
But if you want to use your string, you can splice it into two (space-separated) parts:
t = t.split() # t = ['2018-12-16', '17:59:00']
Then take the first part:
date = t[0]

Python getting hour from from timestamp

I need to classify timestamps based on the hour, for example if it is between 7 AM and 9 AM, it will be morning. I have timestamps that I take from my csv file and I need to get only hour so I can classify the number with if statements.
I will take the timestamps from date column and create a new column named hour,
df['hour'] = df.date.dt.hour
but it gives me the following error: AttributeError: Can only use .dt accessor with datetimelike values
Timestamps are like the following: 2016-03-14 17:24:55
I'm not sure what kind of object is df but you could convert timestamps to datetime objects and then use attributes of datetime objects to access timestamp attributes:
from datetime import datetime
d = datetime.strptime('2016-03-14 17:24:55', '%Y-%m-%d %H:%M:%S')
df['hour'] = d.hour
You can read more about datetime module at this link
You need to convert your 'date' columnn to a datatime object first:
df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d %H:%M:%S')
df['hour'] = df['date'].dt.hour
You need to create a datetime object with the timestamp string extracted from your CSV input data:
In [1]: import datetime
In [2]: s = '2016-03-14 17:24:55'
In [3]: d = datetime.datetime.fromisoformat(s)
In [4]: d.hour
Out[4]: 17
The reason why you get an AttributeError: Can only use .dt accessor with datetimelike values is most likely because that particular series is not of datetime object type.
Like the error states, .dt attribute is available for datetime objects. So first thing to do is check the type of entries.
Suppose the values are not datetime objects then to convert it,
specify datetime_format = '%Y-%m-%d %H:%M:%S' and use .dt the following way to get time values:
data['start_datetime'] = pd.to_datetime(data['start_datetime'], format=datetime_format)
h = data['start_datetime'].dt.hour
m = data['start_datetime'].dt.minute
s = data['start_datetime'].dt.second

Categories

Resources