Snippets

Yuri Zhylyuk Create Calendar Dataframe using Python Pandas

Created by Yuri Zhylyuk

File calendar.py Added

  • Ignore whitespace
  • Hide word diff
+def calendar_df(start='2018-01-01', end='2050-12-31'):
+    from pandas import DataFrame, date_range
+    df = DataFrame({"Date": date_range(start, end)})
+    df["Day"] = df.Date.dt.weekday_name
+    df["Year"] = df.Date.dt.year.astype(int)
+    df["Month"] = df.Date.dt.month.astype(int)
+    df["Year.Month"] = df.Date.dt.strftime("Y%Y.M%m")
+    df["YearISO"] = df.Date.dt.strftime('%G').astype(int)
+    df["Week"] = df.Date.dt.strftime('%V').astype(int)
+    df["Year.Week"] = df.Date.dt.strftime('Y%G.W%V')
+    df["Quarter"] = df.Date.dt.quarter.astype(int)
+    df["YearHalf"] = ((df.Quarter + 1) // 2).astype(int)
+    return df
+    
+    
+from pandas import set_option as pd_option
+pd_option('display.expand_frame_repr', False)
+df = calendar_df()
+print(df.head(20))
+
+#          Date        Day  Year  Month Year.Month  YearISO  Week  Year.Week  Quarter  YearHalf
+# 0  2018-01-01     Monday  2018      1  Y2018.M01     2018     1  Y2018.W01        1         1
+# 1  2018-01-02    Tuesday  2018      1  Y2018.M01     2018     1  Y2018.W01        1         1
+# 2  2018-01-03  Wednesday  2018      1  Y2018.M01     2018     1  Y2018.W01        1         1
+# 3  2018-01-04   Thursday  2018      1  Y2018.M01     2018     1  Y2018.W01        1         1
+# 4  2018-01-05     Friday  2018      1  Y2018.M01     2018     1  Y2018.W01        1         1
+# 5  2018-01-06   Saturday  2018      1  Y2018.M01     2018     1  Y2018.W01        1         1
+# 6  2018-01-07     Sunday  2018      1  Y2018.M01     2018     1  Y2018.W01        1         1
+# 7  2018-01-08     Monday  2018      1  Y2018.M01     2018     2  Y2018.W02        1         1
+# 8  2018-01-09    Tuesday  2018      1  Y2018.M01     2018     2  Y2018.W02        1         1
+# 9  2018-01-10  Wednesday  2018      1  Y2018.M01     2018     2  Y2018.W02        1         1
+# 10 2018-01-11   Thursday  2018      1  Y2018.M01     2018     2  Y2018.W02        1         1
+# 11 2018-01-12     Friday  2018      1  Y2018.M01     2018     2  Y2018.W02        1         1
+# 12 2018-01-13   Saturday  2018      1  Y2018.M01     2018     2  Y2018.W02        1         1
+# 13 2018-01-14     Sunday  2018      1  Y2018.M01     2018     2  Y2018.W02        1         1
+# 14 2018-01-15     Monday  2018      1  Y2018.M01     2018     3  Y2018.W03        1         1
+# 15 2018-01-16    Tuesday  2018      1  Y2018.M01     2018     3  Y2018.W03        1         1
+# 16 2018-01-17  Wednesday  2018      1  Y2018.M01     2018     3  Y2018.W03        1         1
+# 17 2018-01-18   Thursday  2018      1  Y2018.M01     2018     3  Y2018.W03        1         1
+# 18 2018-01-19     Friday  2018      1  Y2018.M01     2018     3  Y2018.W03        1         1
+# 19 2018-01-20   Saturday  2018      1  Y2018.M01     2018     3  Y2018.W03        1         1
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.