Python datetime strptime() and strftime(): how to preserve the timezone information

Part of the problem here is that the strings usually used to represent timezones are not actually unique. “EST” only means “America/New_York” to people in North America. This is a limitation in the C time API, and the Python solution is… to add full tz features in some future version any day now, if anyone is … 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 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

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

Calculate Time Difference Between Two Pandas Columns in Hours and Minutes

Pandas timestamp differences returns a datetime.timedelta object. This can easily be converted into hours by using the *as_type* method, like so import pandas df = pandas.DataFrame(columns=[‘to’,’fr’,’ans’]) df.to = [pandas.Timestamp(‘2014-01-24 13:03:12.050000’), pandas.Timestamp(‘2014-01-27 11:57:18.240000’), pandas.Timestamp(‘2014-01-23 10:07:47.660000’)] df.fr = [pandas.Timestamp(‘2014-01-26 23:41:21.870000’), pandas.Timestamp(‘2014-01-27 15:38:22.540000’), pandas.Timestamp(‘2014-01-23 18:50:41.420000’)] (df.fr-df.to).astype(‘timedelta64[h]’) to yield, 0 58 1 3 2 8 dtype: float64

How to calculate the time interval between two time strings

Yes, definitely datetime is what you need here. Specifically, the datetime.strptime() method, which parses a string into a datetime object. from datetime import datetime s1 = ’10:33:26′ s2 = ’11:15:49′ # for example FMT = ‘%H:%M:%S’ tdelta = datetime.strptime(s2, FMT) – datetime.strptime(s1, FMT) That gets you a timedelta object that contains the difference between the … Read more

How to convert a UTC datetime to a local datetime using only standard library?

In Python 3.3+: from datetime import datetime, timezone def utc_to_local(utc_dt): return utc_dt.replace(tzinfo=timezone.utc).astimezone(tz=None) In Python 2/3: import calendar from datetime import datetime, timedelta def utc_to_local(utc_dt): # get integer timestamp to avoid precision lost timestamp = calendar.timegm(utc_dt.timetuple()) local_dt = datetime.fromtimestamp(timestamp) assert utc_dt.resolution >= timedelta(microseconds=1) return local_dt.replace(microsecond=utc_dt.microsecond) Using pytz (both Python 2/3): import pytz local_tz = pytz.timezone(‘Europe/Moscow’) # … Read more

Convert DataFrame column type from string to datetime

The easiest way is to use to_datetime: df[‘col’] = pd.to_datetime(df[‘col’]) It also offers a dayfirst argument for European times (but beware this isn’t strict). Here it is in action: In [11]: pd.to_datetime(pd.Series([’05/23/2005′])) Out[11]: 0 2005-05-23 00:00:00 dtype: datetime64[ns] You can pass a specific format: In [12]: pd.to_datetime(pd.Series([’05/23/2005′]), format=”%m/%d/%Y”) Out[12]: 0 2005-05-23 dtype: datetime64[ns]