简体   繁体   中英

For loop gets AttributeError: 'Series' object has no attribute 'days'

I am using start_date and end_date Data frames but I'm running into an issue with my loop returning the following error: AttributeError: 'Series' object has no attribute 'days'

I have tried to add.iterrows(), .dt.days, pandas.Series.iteritems without success

# Function to get a list of all dates that are to be downloaded
def get_dates():
    df1 = pd.read_csv(r"C:\Users\14385\PycharmProjects\execvet_actrivist_db\short_seller_database.csv")
    raw_start_date = df1['Publication Date']
    start_date= pd.to_datetime(raw_start_date)
    ticker = df1['Ticker']
    end_date = start_date + timedelta(days=365)  # One year trailing calendar days acc. to IEX docu

    **dates = [start_date + timedelta(days=i) for i in range((end_date - start_date).days)] ##.dt.days Not working either**

I also tried to convert the series to a data frame ( dates = [start_date + timedelta(days=i) for i in range((end_date.to_frame() - start_date.to_frame))]) but failed

Thanks for any help!

Some corrections here, timedelta(days=365) will not add a full year if for example the next year is a leap-year. And second start_date and end_date should be datetime series, the subtraction is a TimeDelta object so days should be fine:

from pandas.tseries.offsets import DateOffset

# ....

start_date= pd.to_datetime(raw_start_date)
end_date = start_date + DateOffset(years=1)
dates = [start_date + DateOffset(days=i) \
    for i in (end_date - start_date).days]

I know what the dates matrix is for, but assuming the dates matrix is generated each day from start to end for each record in start you can use daterange :

dates = [pd.date_range(start, start + DateOffset(years=1), freq='1D')\
        for start in start_date]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM