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
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)
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)
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>)
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')
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
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)
You can just use
datetime.date.today()
It's light and returns exactly what you want.
Here is yet another way which fits in one line but is not particularly elegant:
dt = datetime.datetime.fromordinal(datetime.date.today().toordinal())
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
datetime
object without passing time properties to the constructor, you get midnight.dt = datetime.datetime.now()
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
"""
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?
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
There is a module datetime_truncate which handlers this for you. It just calls datetime.replace.
>>> import datetime
>>> dt = datetime.datetime.now()
>>> datetime.datetime.date(dt)
datetime.date(2019, 4, 2)
Source: Stackoverflow.com