How to make a datetime object aware (not naive)

In general, to make a naive datetime timezone-aware, use the localize method: import datetime import pytz unaware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0) aware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0, pytz.UTC) now_aware = pytz.utc.localize(unaware) assert aware == now_aware For the UTC timezone, it is not really necessary to use localize since … Read more

Pandas: convert date in month to the 1st day of next month

You can use pd.offsets.MonthBegin() In [261]: d = pd.to_datetime([‘2011-09-30’, ‘2012-02-28’]) In [262]: d Out[262]: DatetimeIndex([‘2011-09-30’, ‘2012-02-28’], dtype=”datetime64[ns]”, freq=None) In [263]: d + pd.offsets.MonthBegin(1) Out[263]: DatetimeIndex([‘2011-10-01’, ‘2012-03-01′], dtype=”datetime64[ns]”, freq=None) You’ll find a lot of examples in the official Pandas docs

How to make a datetime object aware (not naive) in Python?

In general, to make a naive datetime timezone-aware, use the localize method: import datetime import pytz unaware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0) aware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0, pytz.UTC) now_aware = pytz.utc.localize(unaware) assert aware == now_aware For the UTC timezone, it is not really necessary to use localize since … Read more

How to convert integer into date object python?

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 -= … Read more

Why is pandas.to_datetime slow for non standard time format such as ‘2014/12/31’

This is because pandas falls back to dateutil.parser.parse for parsing the strings when it has a non-default format or when no format string is supplied (this is much more flexible, but also slower). As you have shown above, you can improve the performance by supplying a format string to to_datetime. Or another option is to … Read more

How do I determine if current time is within a specified range using Python’s datetime module?

My original answer focused very specifically on the question as posed and didn’t accommodate time ranges that span midnight. As this is still the accepted answer 6 years later, I’ve incorporated @rouble’s answer below that expanded on mine to support midnight. from datetime import datetime, time def is_time_between(begin_time, end_time, check_time=None): # If check time is … Read more

How do I round datetime column to nearest quarter hour

You can use round(freq). There is also a shortcut column.dt for datetime functions access (as @laurens-koppenol suggests). Here’s one-liner: df[‘old column’].dt.round(’15min’) String aliases for valid frequencies can be found here. Full working example: In [1]: import pandas as pd In [2]: df = pd.DataFrame([pd.Timestamp(‘2015-07-18 13:53:33.280’), pd.Timestamp(‘2015-07-18 13:33:33.330’)], columns=[‘old column’]) In [3]: df[‘new column’]=df[‘old column’].dt.round(’15min’) In … Read more