What is a classy way to way truncate a python datetime object?
In this particular case, to the day. So basically setting hour, minute, seconds, and microseconds to 0.
I would like the output to also be a datetime object, not a string.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I think this is what you’re looking for…
>>> import datetime >>> dt = datetime.datetime.now() >>> dt = dt.replace(hour=0, minute=0, second=0, microsecond=0) # Returns a copy >>> dt datetime.datetime(2011, 3, 29, 0, 0)
But if you really don’t care about the time aspect of things, then you should really only be passing around date objects…
>>> d_truncated = datetime.date(dt.year, dt.month, dt.day) >>> d_truncated datetime.date(2011, 3, 29)
Method 2
Use a date not a datetime if you dont care about the time.
>>> now = datetime.now() >>> now.date() datetime.date(2011, 3, 29)
You can update a datetime like this:
>>> now.replace(minute=0, hour=0, second=0, microsecond=0) datetime.datetime(2011, 3, 29, 0, 0)
Method 3
Four years later: another way, avoiding replace
I know the accepted answer from four years ago works, but this seems a tad lighter than using replace:
dt = datetime.date.today() dt = datetime.datetime(dt.year, dt.month, dt.day)
Notes
- When you create a
datetimeobject without passing time properties to the constructor, you get midnight. - As others have noted, this assumes you want a datetime object for later use with timedeltas.
- You can, of course, substitute this for the first line:
dt = datetime.datetime.now()
Method 4
You cannot truncate a datetime object because it is immutable.
However, here is one way to construct a new datetime with 0 hour, minute, second, and microsecond fields, without throwing away the original date or tzinfo:
newdatetime = now.replace(hour=0, minute=0, second=0, microsecond=0)
Method 5
To get a midnight corresponding to a given datetime object, you could use datetime.combine() method:
>>> from datetime import datetime, time >>> dt = datetime.utcnow() >>> dt.date() datetime.date(2015, 2, 3) >>> datetime.combine(dt, time.min) datetime.datetime(2015, 2, 3, 0, 0)
The advantage compared to the .replace() method is that datetime.combine()-based solution will continue to work even if datetime module introduces the nanoseconds support.
tzinfo can be preserved if necessary but the utc offset may be different at midnight e.g., due to a DST transition and therefore a naive solution (setting tzinfo time attribute) may fail. See How do I get the UTC time of “midnight” for a given timezone?
Method 6
You could use pandas for that (although it could be overhead for that task). You could use round, floor and ceil like for usual numbers and any pandas frequency from offset-aliases:
import pandas as pd import datetime as dt now = dt.datetime.now() pd_now = pd.Timestamp(now) freq = '1d' pd_round = pd_now.round(freq) dt_round = pd_round.to_pydatetime() print(now) print(dt_round) """ 2018-06-15 09:33:44.102292 2018-06-15 00:00:00 """
Method 7
See more at https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.floor.html
It’s now 2019, I think the most efficient way to do it is:
df['truncate_date'] = df['timestamp'].dt.floor('d')
Method 8
There is a great library used to manipulate dates: Delorean
import datetime
from delorean import Delorean
now = datetime.datetime.now()
d = Delorean(now, timezone='US/Pacific')
>>> now
datetime.datetime(2015, 3, 26, 19, 46, 40, 525703)
>>> d.truncate('second')
Delorean(datetime=2015-03-26 19:46:40-07:00, timezone='US/Pacific')
>>> d.truncate('minute')
Delorean(datetime=2015-03-26 19:46:00-07:00, timezone='US/Pacific')
>>> d.truncate('hour')
Delorean(datetime=2015-03-26 19:00:00-07:00, timezone='US/Pacific')
>>> d.truncate('day')
Delorean(datetime=2015-03-26 00:00:00-07:00, timezone='US/Pacific')
>>> d.truncate('month')
Delorean(datetime=2015-03-01 00:00:00-07:00, timezone='US/Pacific')
>>> d.truncate('year')
Delorean(datetime=2015-01-01 00:00:00-07:00, timezone='US/Pacific')
and if you want to get datetime value back:
>>> d.truncate('year').datetime
datetime.datetime(2015, 1, 1, 0, 0, tzinfo=<DstTzInfo 'US/Pacific' PDT-1 day, 17:00:00 DST>)
Method 9
You can use datetime.strftime to extract the day, the month, the year…
Example :
from datetime import datetime
d = datetime.today()
# Retrieves the day and the year
print d.strftime("%d-%Y")
Output (for today):
29-2011
If you just want to retrieve the day, you can use day attribute like :
from datetime import datetime d = datetime.today() # Retrieves the day print d.day
Ouput (for today):
29
Method 10
If you are dealing with a Series of type DateTime there is a more efficient way to truncate them, specially when the Series object has a lot of rows.
You can use the floor function
For example, if you want to truncate it to hours:
Generate a range of dates
times = pd.Series(pd.date_range(start='1/1/2018 04:00:00', end='1/1/2018 22:00:00', freq='s'))
We can check it comparing the running time between the replace and the floor functions.
%timeit times.apply(lambda x : x.replace(minute=0, second=0, microsecond=0))
>>> 341 ms ± 18.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit times.dt.floor('h')
>>>>2.26 ms ± 451 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Method 11
>>> import datetime >>> dt = datetime.datetime.now() >>> datetime.datetime.date(dt) datetime.date(2019, 4, 2)
Method 12
There is a module datetime_truncate which handlers this for you. It just calls datetime.replace.
Method 13
6 years later… I found this post and I liked more the numpy aproach:
import numpy as np
dates_array = np.array(['2013-01-01', '2013-01-15', '2013-01-30']).astype('datetime64[ns]')
truncated_dates = dates_array.astype('datetime64[D]')
cheers
Method 14
Here is yet another way which fits in one line but is not particularly elegant:
dt = datetime.datetime.fromordinal(datetime.date.today().toordinal())
Method 15
If you want to truncate to an arbitrary timedelta:
from datetime import datetime, timedelta truncate = lambda t, d: t + (datetime.min - t) % - d # 2022-05-04 15:54:19.979349 now = datetime.now() # truncates to the last 15 secondes print(truncate(now, timedelta(seconds=15))) # truncates to the last minute print(truncate(now, timedelta(minutes=1))) # truncates to the last 2 hours print(truncate(now, timedelta(hours=2))) # ... """ 2022-05-04 15:54:15 2022-05-04 15:54:00 2022-05-04 14:00:00 """
PS: This is for python3
Method 16
You can just use
datetime.date.today()
It’s light and returns exactly what you want.
Method 17
What does truncate mean?
You have full control over the formatting by using the strftime() method and using an appropriate format string.
http://docs.python.org/library/datetime.html#strftime-strptime-behavior
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0