This question already has answers here:
Python pandas integer YYYYMMDD to datetime
(2 answers)
Closed 5 months ago.
I am trying to convert a pandas column DateTime (UTC) which seems like this df_1['MESS_DATUM'] = 202209250000 to unixtime. My code looks like this:
df_1['MESS_DATUM'] = calendar.timegm(df_1['MESS_DATUM'].timetuple())
print(datetime.utcfromtimestamp(df_1['MESS_DATUM']))
But I am getting this error "AttributeError: 'Series' object has no attribute 'timetuple'".
I have used the below method as well but my time is in UTC which is why it is not giving me the right unix time I guess:
df_1['MESS_DATUM'] = pd.to_datetime(df_1['MESS_DATUM'])
(df_1['MESS_DATUM'] - pd.Timestamp("1970-01-01")) // pd.Timedelta('1s')
print(df_1['MESS_DATUM']) #it gives me the following datetime in unix form
1970-01-01 00:03:22.209252150
I tried the above method for a single datetime string as shown below and it works but for the whole datetime column it gives me this value 1970-01-01 00:03:22.209252150
dates = pd.to_datetime(['2022-09-15 13:30:00'])
# calculate unix datetime
dates = (dates - pd.Timestamp("1970-01-01")) // pd.Timedelta('1s')
print(dates) # Int64Index([1663248600], dtype='int64')
I tried this method as well which gives me again the wrong unixtime
df_1['MESS_DATUM'] = pd.DatetimeIndex ( df_1['MESS_DATUM'] ).astype ( np.int64 )/1000000
print(df_1['MESS_DATUM'])
202.209252 # this is the unixtime I get
Any helpful solution will be highly appreciated.
You could convert the value using the datetime library;
d = 202209250000
import datetime
datetime.datetime.strptime(str(d),'%Y%m%d%H%M').timestamp()
Converting the column can be done using using df.apply;
df = pd.DataFrame({'MESS_DATUM': [202209250000,202209260000,202209270000]})
df['MESS_DATUM'] = df['MESS_DATUM'].apply(lambda x: datetime.datetime.strptime(str(x),'%Y%m%d%H%M').timestamp())
Related
This question already has answers here:
removing time from date&time variable in pandas?
(3 answers)
Closed last year.
solar["DATE"]= solar['DATE'].strftime('%Y-%m-%d')
display(solar)
I want to remove the time function from the DATE column. I only want the date, how do I get rid of it but keep the date?
[1]: https://i.stack.imgur.com/8G8Jg.png
The error I get is below:
AttributeError: 'Series' object has no attribute 'strftime'
According to the error i think so you are using pandas dataframe and to edit the values you will have to use .apply() function.
You can do it via:
#IF the values are already a datetime object
solar['DATE'].apply(lambda d: d.date())
#ELSE IF dates are a string:
solar['DATE'].apply(lambda d: d.stftime('%Y-%m-%d'))
What I came up with is what follows:
import pandas as pd
import datetime
date = pd.date_range("2018-01-01", periods=500, freq="H")
dataframe = pd.DataFrame({"date":date})
def removeDayTime(date):
dateStr = str(date) # This line is just to change the timestamp format to str. You probably do not need this line to include in your code.
dateWitoutTime = datetime.datetime.strptime(dateStr, "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d")
return dateWitoutTime
dataframe["date"] = dataframe["date"].apply(removeDayTime)
dataframe.head()
Note that in order to have example data to work with, I have generated 500 periods of dates. You probably do not need to use my dataframe. So just use the rest of the code.
Output
date
0
2018-01-01
1
2018-01-01
2
2018-01-01
3
2018-01-01
4
2018-01-01
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()
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
I am converting all ISO-8601 formatted values into Unix Values. For some inexplicable reason this line
a_col = pd.DatetimeIndex(a_col).astype(np.int64)/10**6
raises the error
ValueError: Unable to convert 0 2001-06-29
... (Abbreviated Output of Column
Name: DateCol, dtype: datetime64[ns] to datetime dtype
This is very odd because I've guaranteed that each value is in datetime.datetime format as you can see here:
if a_col.dtypes is (np.dtype('object') or np.dtype('O')):
a_col = a_col.apply(lambda x: x if isinstance(x, datetime.datetime) else epoch)
a_col = pd.DatetimeIndex(a_col).astype(np.int64)/10**6
Epoch is datetime.datetime.
When I check the dtypes of the column that gives me an error it's "object), exactly what I'm checking for. Is there something I'm missing?
Assuming that your time zone is US/Eastern (based on your dataset) and that your DataFrame is named df, please try the following:
import datetime as dt
from time import mktime
import pytz
df['Job Start Date'] = \
df['Job Start Date'].apply(lambda x: mktime(pytz.timezone('US/Eastern').localize(x)
.astimezone(pytz.UTC).timetuple()))
>>> df['Job Start Date'].head()
0 993816000
1 1080824400
2 1052913600
3 1080824400
4 1075467600
Name: Job Start Date, dtype: float64
You first need to make your 'naive' datetime objects timezone aware (to US/Eastern) and then convert them to UTC. Finally, pass your new UTC aware datetime object as a timetable to the mtkime function from the time module.
I am creating a module in python, in which I am receiving the date in integer format like 20120213, which signifies the 13th of Feb, 2012. Now, I want to convert this integer formatted date into a python date object.
Also, if there is any means by which I can subtract/add the number of days in such integer formatted date to receive the date value in same format? like subtracting 30 days from 20120213 and receive answer as 20120114?
This question is already answered, but for the benefit of others looking at this question I'd like to add the following suggestion: Instead of doing the slicing yourself as suggested in the accepted answer, you might also use strptime() which is (IMHO) easier to read and perhaps the preferred way to do this conversion.
import datetime
s = "20120213"
s_datetime = datetime.datetime.strptime(s, '%Y%m%d')
I would suggest the following simple approach for conversion:
from datetime import datetime, timedelta
s = "20120213"
# you could also import date instead of datetime and use that.
date = datetime(year=int(s[0:4]), month=int(s[4:6]), day=int(s[6:8]))
For adding/subtracting an arbitary amount of days (seconds work too btw.), you could do the following:
date += timedelta(days=10)
date -= timedelta(days=5)
And convert back using:
s = date.strftime("%Y%m%d")
To convert the integer to a string safely, use:
s = "{0:-08d}".format(i)
This ensures that your string is eight charecters long and left-padded with zeroes, even if the year is smaller than 1000 (negative years could become funny though).
Further reference: datetime objects, timedelta objects
Here is what I believe answers the question (Python 3, with type hints):
from datetime import date
def int2date(argdate: int) -> date:
"""
If you have date as an integer, use this method to obtain a datetime.date object.
Parameters
----------
argdate : int
Date as a regular integer value (example: 20160618)
Returns
-------
dateandtime.date
A date object which corresponds to the given value `argdate`.
"""
year = int(argdate / 10000)
month = int((argdate % 10000) / 100)
day = int(argdate % 100)
return date(year, month, day)
print(int2date(20160618))
The code above produces the expected 2016-06-18.
import datetime
timestamp = datetime.datetime.fromtimestamp(1500000000)
print(timestamp.strftime('%Y-%m-%d %H:%M:%S'))
This will give the output:
2017-07-14 08:10:00