Managing Date, Datetime, and Timestamp in Python/Pandas

Managing Date, Datetime, and Timestamp in Python/Pandas

Personal documentation for managing date & time in python/pandas. This is a living document to assist analysis projects in Jupyter Notebook.

?

Table of Contents:

  1. What is today?s date?
  2. Timedeltas
  3. Convert Datetime to Date
  4. Create new [?day?],[?week?],[?month?], or [?year?]column from Datetime
  5. Convert String to Timestamp
  6. Convert String to Datetime
  7. Convert Dataframe String Date Column to Datetime
  8. Strip Date String Data
  9. Convert Timestamp to String
  10. Convert String Column to Datetime
  11. Convert Timestamp to Datetime
  12. Unix Date Modifiers

p.s. just an idea: if you get some value out of this article, and you?d like to support the author, purchase your next Amazon shipment through my affiliate link here. Cheers!

1. What is today?s date?

# gives timestamp of today’s date at the time of cell executionimport pandas as pdtoday = pd.to_datetime(‘today’)Image for post

2. Timedeltas

# using timedelta on a datetime from datetime import timedeltatoday = pd.to_datetime(‘today’)last_week = today + timedelta(days=-7)# this will return a timestamp

3. Convert Datetime to Date

from datetime import datetime as dtdf[‘date’] = df[‘date_time’].dt.normalize()

4. Create new [?day?],[?week?],[?month?], or [?year?]column from Datetime

df[‘week’] = df[‘date_time’].dt.weekdf[‘month’] = df[‘date_time’].dt.monthdf[‘year’] = df[‘date_time’].dt.yeardf[‘week-year’] = df[‘date_time’].dt.strftime(‘%Y-%U’)df[‘month-year’] = df[‘date_time’].dt.strftime(‘%Y-%m’)

Another way to approach this (if you get error: Can only use .dt accessor with datetimelike values):

df[‘month’] = pd.DatetimeIndex(df[‘start_date’]).monthdf[‘year’] = pd.DatetimeIndex(df[‘start_date’]).year

resources:

  • https://www.interviewqs.com/ddi_code_snippets/extract_month_year_pandas
  • https://stackoverflow.com/questions/39208305/pandas-how-to-create-a-year-week-variable
  • https://stackoverflow.com/questions/31984119/python-how-to-know-the-week-number-in-the-year-of-the-day-saturday-as-the-firs
  • https://stackoverflow.com/questions/31181295/converting-a-pandas-date-to-week-number
  • https://www.programiz.com/python-programming/datetime/strftime

5. Convert String to Timestamp

import timeimport datetimes = “01/12/2011″time_stamp = time.mktime(datetime.datetime.strptime(s, “%d/%m/%Y”).timetuple())

  • https://stackoverflow.com/questions/9637838/convert-string-date-to-timestamp-in-python

6. Convert String to Datetime

date_string = ‘2019-04-17’time_stamp = pd.to_datetime(date_string)date_time = time_stamp.date()

7. Convert Dataframe String Column to Datetime

df[‘datetime’] = pd.to_datetime(df.date_string)time_stamp = pd.to_datetime(date_string)date_time = time_stamp.date()

8. Strip Date String Data

date_string = ‘2020-07-26T00:00:00’df[‘date_string’].str.strip(‘T00:00:00’)

9. Convert Timestamp or Datetime to String

str(timestamp)#output will look like: ‘2019-11-02 11:26:58.924878’str(timestamp)[:-16]#output will look like: ‘2019-11-02’#then you can evalute <, >, = vs. other date stringssometimes you need to mess around with the values x & y here to get the string you want… str(timestamp)[X:Y]for example, str(timestamp)[1:18] may be required in some instances, etc.

10. Convert String Column to Datetime

df[‘date_time’] = df[‘string_date’].astype(‘datetime64[ns]’)

11. Convert Timestamp to Datetime

date_time = time_stamp.date()

12. Unix Date Modifiers

%% a literal %%a locale’s abbreviated weekday name (e.g., Sun)%A locale’s full weekday name (e.g., Sunday)%b locale’s abbreviated month name (e.g., Jan)%B locale’s full month name (e.g., January)%c locale’s date and time (e.g., Thu Mar 3 23:05:25 2005)%C century; like %Y, except omit last two digits (e.g., 20)%d day of month (e.g., 01)%D date; same as %m/%d/%y%e day of month, space padded; same as %_d%F full date; like %+4Y-%m-%d%g last two digits of year of ISO week number (see %G)%G year of ISO week number (see %V); normally useful only with %V%h same as %b%H hour (00..23)%I hour (01..12)%j day of year (001..366)%k hour, space padded ( 0..23); same as %_H%l hour, space padded ( 1..12); same as %_I%m month (01..12)%M minute (00..59)%n a newline%N nanoseconds (000000000..999999999)%p locale’s equivalent of either AM or PM; blank if not known%P like %p, but lower case%q quarter of year (1..4)%r locale’s 12-hour clock time (e.g., 11:11:04 PM)%R 24-hour hour and minute; same as %H:%M%s seconds since 1970-01-01 00:00:00 UTC%S second (00..60)%t a tab%T time; same as %H:%M:%S%u day of week (1..7); 1 is Monday%U week number of year, with Sunday as first day of week (00..53)%V ISO week number, with Monday as first day of week (01..53)%w day of week (0..6); 0 is Sunday%W week number of year, with Monday as first day of week (00..53)%x locale’s date representation (e.g., 12/31/99)%X locale’s time representation (e.g., 23:13:48)%y last two digits of year (00..99)%Y year%z +hhmm numeric time zone (e.g., -0400)%:z +hh:mm numeric time zone (e.g., -04:00)%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)%:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)%Z alphabetic time zone abbreviation (e.g., EDT)

  • Source: http://man7.org/linux/man-pages/man1/date.1.html

Thanks for reading! I hope this article was helpful.

If you want to support the author, the best way is to purchase your next Amazon shipment through my affiliate link here. Or, consider buying me a coffee through venmo (David-Allen-13), cash app (deallen7), or paypal (davideallen).

Your donations, comments, and claps keep me motivated to create more material. I appreciate you! 😀

23