Creating a date range in Pandas/Python with just the year

I have a dataframe, df. This dataframe includes a column called "Accession Year." I need to create a date range. I’ve tried running this code, but it didn’t work

index = df.AccessionYear.date_range(start = '1877',end='2020') 

Unfortunately, this column does not have any months or days, only the year. The error message that I am getting is " AttributeError: ‘Series’ object has no attribute ‘date_range’". Would anyone happen to know any other methods?

Add Comment
2 Answer(s)

date_range is a method coming from pandas class, you should do:

import pandas as pd index = pd.date_range(start = '1877',end='2020') 
Answered on July 16, 2020.
Add Comment

Read the error message carefully, and think about what you coded.

Accession_Year is a column of strings (inferring from your attempt to call the method). Why do you think a column of strings has a method date_range?

If you need an absolute date range, as indicated in your post, why did you insert Accession_Year into the call? You use simply

pd.date_range(start='1877', end='2020') 

If the column does affect the call, you need to explain how, such as

pd.date_range(start=df.Accession_Year.min(), end='2020') 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.