Skip to content

Instantly share code, notes, and snippets.

@thekensta
Last active November 25, 2015 12:37
Show Gist options
  • Save thekensta/68e2cc4793fa8f847842 to your computer and use it in GitHub Desktop.
Save thekensta/68e2cc4793fa8f847842 to your computer and use it in GitHub Desktop.
Numpy and Pandas date arithmetic
import pandas as pd
import numpy as np
#
td = pd.DataFrame({'Date': pd.date_range('2014-01-01', '2015-12-31')})
td['Timedelta'] = td['Date'].max() - td['Date']
td.dtypes
# Date datetime64[ns]
# Timedelta timedelta64[ns]
# dtype: object
td["AgeDays"] = td["Timedelta"] / np.timedelta64(1, 'D')
td["AgeMonths"] = td["Timedelta"] / np.timedelta64(1, 'M')
td["AgeYears"] = td["Timedelta"] / np.timedelta64(1, 'Y')
td.head()
# Date Timedelta AgeDays AgeMonths AgeYears
# 0 2014-01-01 729 days 729 23.951210 1.995934
# 1 2014-01-02 728 days 728 23.918356 1.993196
# 2 2014-01-03 727 days 727 23.885501 1.990458
# 3 2014-01-04 726 days 726 23.852646 1.987720
# 4 2014-01-05 725 days 725 23.819791 1.984983
# Days in a month
(td["AgeDays"] / td["AgeMonths"]).head()
# 0 30.436875
# 1 30.436875
# Days in a Year
(td["AgeDays"] / td["AgeYears"]).head()
# 0 365.2425
# 1 365.2425
## PERIODS
td["Month"] = td["Date"].dt.to_period("M")
td["Year"] = td["Date"].dt.to_period("Y")
td.head()
# Date Timedelta AgeDays Month Year
# 0 2014-01-01 729 days 729 2014-01 2014
# 1 2014-01-02 728 days 728 2014-01 2014
## TYPES
td.dtypes
# Date datetime64[ns]
# Timedelta timedelta64[ns]
# AgeDays float64
# Month object
# Year object
# dtype: object
d0 = td["Date"].iloc[0]
p0 = td["Month"].iloc[0]
type(d0)
# pandas.tslib.Timestamp
type(p0)
# pandas._period.Period
# Work out the first date of the week or first date of the Month
week_start = pd.offsets.Week(weekday=0)
month_begin = pd.offsets.MonthBegin()
td["WeekStart"] = td.Date.apply(lambda x: week_start.rollback(x))
td["MonthStart"] = td.Date.apply(lambda x: month_begin.rollback(x))
print(td[["Date", "MonthStart", "WeekStart"]].head(25))
# Date MonthStart WeekStart
# 0 2014-01-01 2014-01-01 2013-12-30
# 1 2014-01-02 2014-01-01 2013-12-30
# 2 2014-01-03 2014-01-01 2013-12-30
# 3 2014-01-04 2014-01-01 2013-12-30
# 4 2014-01-05 2014-01-01 2013-12-30
# 5 2014-01-06 2014-01-01 2014-01-06
# 6 2014-01-07 2014-01-01 2014-01-06
# 7 2014-01-08 2014-01-01 2014-01-06
# 8 2014-01-09 2014-01-01 2014-01-06
# 9 2014-01-10 2014-01-01 2014-01-06
# 10 2014-01-11 2014-01-01 2014-01-06
# 11 2014-01-12 2014-01-01 2014-01-06
# 12 2014-01-13 2014-01-01 2014-01-13
# 13 2014-01-14 2014-01-01 2014-01-13
# 14 2014-01-15 2014-01-01 2014-01-13
# 15 2014-01-16 2014-01-01 2014-01-13
# 16 2014-01-17 2014-01-01 2014-01-13
# 17 2014-01-18 2014-01-01 2014-01-13
# 18 2014-01-19 2014-01-01 2014-01-13
# 19 2014-01-20 2014-01-01 2014-01-20
# 20 2014-01-21 2014-01-01 2014-01-20
# 21 2014-01-22 2014-01-01 2014-01-20
# 22 2014-01-23 2014-01-01 2014-01-20
# 23 2014-01-24 2014-01-01 2014-01-20
# 24 2014-01-25 2014-01-01 2014-01-20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment