Paras Ghai
Paras Ghai

Reputation: 373

Generate Date Ranges in python on yearly basis?

I have a problem statement in which I want the user to input only month and year :- like "Jan" and "2018" . Now, I want the most optimized way to generate the date range in python for that whole year. I this case I should get list of all dates:-

          Date
1   2018-01-01
2   2018-01-02
3   2018-01-03
4   2018-01-04
5   2018-01-05
6   2018-01-06
7   2018-01-07
8   2018-01-08
9   2018-01-09
10  2018-01-10
11  2018-01-11
12  2018-01-12
13  2018-01-13
14  2018-01-14  till 2018-12-31

Upvotes: 2

Views: 1200

Answers (3)

Nils Werner
Nils Werner

Reputation: 36765

Because using Timedelta for very large deltas seems to be imprecise, you can also use DateOffset to perform the date difference calculations:

import pandas as pd
import pandas.tseries.offsets as po

m = 'Mar'
y = 2019

start = pd.to_datetime(f'{m}{y}', format='%b%Y')
end = start + po.DateOffset(years=1) - po.DateOffset(days=1)

df = pd.DataFrame({'Date': pd.date_range(start, end)})
df
# Date
# 0 2019-03-01
# 1 2019-03-02
# 2 2019-03-03
# 3 2019-03-04
# 4 2019-03-05
# ...   ...
# 361   2020-02-25
# 362   2020-02-26
# 363   2020-02-27
# 364   2020-02-28
# 365   2020-02-29
# 366 rows × 1 columns

Upvotes: 1

jezrael
jezrael

Reputation: 862691

Use date_range with specify start and end datetimes:

m = 'Mar'
y = 2019

start = pd.to_datetime(f'{m}{y}', format='%b%Y')
end = pd.to_datetime(f'{m}{y + 1}', format='%b%Y')  - pd.Timedelta('1d')

df = pd.DataFrame({'Date': pd.date_range(start, end)})

print (df)
          Date
0   2019-03-01
1   2019-03-02
2   2019-03-03
3   2019-03-04
4   2019-03-05
..         ...
361 2020-02-25
362 2020-02-26
363 2020-02-27
364 2020-02-28
365 2020-02-29

[366 rows x 1 columns]

EDIT:

For weekends filter by Series.dt.dayofweek:

df1 = df[df['Date'].dt.dayofweek > 4]
print (df1)
          Date
1   2019-03-02
2   2019-03-03
8   2019-03-09
9   2019-03-10
15  2019-03-16
..         ...
351 2020-02-15
352 2020-02-16
358 2020-02-22
359 2020-02-23
365 2020-02-29

[105 rows x 1 columns]

Upvotes: 2

Dev Khadka
Dev Khadka

Reputation: 5451

Just do the following, pandas date_range will do the rest

year = 2019
month="jan"
pd.date_range(f"{year}-{month}-1", f"{year}-{12}-31")

Upvotes: 0

Related Questions