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
Related
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()
With python, How can I check if a date stored in a string has already passed?
My current code:
from datetime import date, datetime
date1 = date.today()
data2_str = '2018-06-25'
data2_obj = datetime.strptime(data2_str, '%Y-%m-%d')
print(date1<=data2_obj)
The code above gives me the following error:
TypeError: can't compare datetime.datetime to datetime.date
Note that I would not want to work with any time - just the date (this case the treated in 32287708)
Use the .date() method to get the date component like this:
from datetime import date, datetime
date1 = date.today()
date2_str = '2018-06-25'
date2 = datetime.strptime(date2_str, '%Y-%m-%d').date()
print(date1<=date2)
Output:
False
In a train data set, datetime column is an object . First row of this column : 2009-06-15 17:26:21 UTC . I tried splitting the data
train['Date'] = train['pickup_datetime'].str.slice(0,11)
train['Time'] = test['pickup_datetime'].str.slice(11,19)
So that I can split the Date and time as two variables and change them to datetime data type. Tried lot of methods but could not get the result.
train['Date']=pd.to_datetime(train['Date'], format='%Y-%b-%d')
Also tried spliting the date,time and UTC
train['DateTime'] = pd.to_datetime(train['DateTime'])
Please suggest a code for this. I am a begginer.
Thanks in advance
I would try the following
import pandas as pd
#create some random dates matching your formatting
df = pd.DataFrame({"date": ["2009-06-15 17:26:21 UTC", "2010-08-16 19:26:21 UTC"]})
#convert to datetime objects
df["date"] = pd.to_datetime(df["date"])
print(df["date"].dt.date) #returns the date part without tz information
print(df["date"].dt.time) #returns the time part
Output:
0 2009-06-15
1 2010-08-16
Name: date, dtype: object
0 17:26:21
1 19:26:21
Name: date, dtype: object
For further information feel free to consult the docs:
dt.date
dt.time
For your particular case:
#convert to datetime object
df['pickup_datetime']= pd.to_datetime(df['pickup_datetime'])
# seperate date and time
df['Date'] = df['pickup_datetime'].dt.date
df['Time'] = df['pickup_datetime'].dt.time
I have a date column in my dataframe that consists of strings like this...'201512'
I would like to convert it into a datetime object of just year to do some time series analysis.
I tried...
df['Date']= pd.to_datetime(df['Date'])
and something similar to
datetime.strptime(Date, "%Y")
I am not sure how datetime interfaces with pandas dataframes (perhaps somebody will comment if there is special usage), but in general the datetime functions would work like this:
import datetime
date_string = "201512"
date_object = datetime.datetime.strptime(date_string, "%Y%m")
print(date_object)
Getting us:
2015-12-01 00:00:00
Now that the hard part of creating a datetime object is done we simply
print(date_object.year)
Which spits out our desired
2015
More info about the parsing operators (the "%Y%m" bit of my code) is described in the documentation
I would look at the module arrow
https://arrow.readthedocs.io/en/latest/
import arrow
date = arrow.now()
#example of text formatting
fdate = date.format('YYYY')
#example of converting text into datetime
date = arrow.get('201905', 'YYYYMM').datetime
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]