AttributeError: 'Series' object has no attribute 'isoweekday'

halny

I wrote a function that moves the date by specified number of working days in respect to the weekends and holidays. It works fine when I use it with scalar values however I have issue trying to use on the pandas DataFrame. The function itself looks as follow:

def move_date_by_days(init_date, roll=1, nwd_key=None, hol_key=None):
    '''
    moves date by n-number of working days forward or backward
    init_date: date, initial caluclation date
    roll: integer, number of days to move forward (+) or backward (-)
    nwd_key: string that stands for currency iso code, it is a key in non_working_days dictionary
    hol_key: string that stands for currency iso code, it is a key in holidays dictonary
    return: date
    '''
    nwd = non_working_days.get(nwd_key, [])
    hol = holidays.get(hol_key,[])
    moved_date = init_date + datetime.timedelta(days=roll)
    if (moved_date.isoweekday() in nwd) or (moved_date in hol):
        if roll >= 0:
            moved_date=move_date_by_days(init_date + datetime.timedelta(days=1), roll=roll, nwd_key=nwd_key, hol_key=hol_key)
        else:
            moved_date=move_date_by_days(init_date + datetime.timedelta(days=-1), roll=roll, nwd_key=nwd_key, hol_key=hol_key)
    return moved_date

Now, I have DataFrame 'df' with the columns 'start_date' and 'end_date'

   start_date    end_date
0  2020-01-31  2020-04-30
1  2020-04-30  2020-07-31
2  2020-07-31  2020-10-31
3  2020-10-31  2020-11-28

and I would like to create the third column named 'fixing' that would be 2 working days before 'start_date'. I am trying this:

dates_table['fixing'] = move_date_by_days(self.dates_table['start_date'], -2, self.ccy, self.ccy)

but it returns AttributeError: 'Series' object has no attribute 'isoweekday'

Please note the function works fine when I refer to scalar data of the DataFrame, for instance

d1 = s1.dates_table.iat[0,0]
move_date_by_days(d1, -2, 'pln', 'pln')

it returns what I have expected: datetime.date(2020, 1, 29)

Any hint how could I use the function on the whole column of the data frame, please?

Chris A

IIUC, why not use offsets.BDay or offsets.CustomBusinessDay which takes a list of holidays:

df['fixing'] = df['start_date'] - pd.offsets.BDay(2)
df['fixing'] = df['start_date'] - pd.offsets.CustomBusinessDay(2, holidays=holidays['pln'])

[out]

  start_date   end_date     fixing
0 2020-01-31 2020-04-30 2020-01-29
1 2020-04-30 2020-07-31 2020-04-28
2 2020-07-31 2020-10-31 2020-07-29
3 2020-10-31 2020-11-28 2020-10-29

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Dask compute gives AttributeError: 'Series' object has no attribute 'encode'

分類Dev

AttributeError: 'module' object has no attribute

分類Dev

AttributeError: 'function' object has no attribute

分類Dev

AttributeError: 'bool' object has no attribute

分類Dev

SQLAlchemy "AttributeError: 'str' object has no attribute 'c'"

分類Dev

TensorFlow:AttributeError: 'Tensor' object has no attribute 'shape'

分類Dev

AttributeError: 'module' object has no attribute 'computation'

分類Dev

Scrapy:AttributeError: 'list' object has no attribute 'iteritems'

分類Dev

AttributeError: 'module' object has no attribute 'reader'

分類Dev

AttributeError: 'list' object has no attribute 'split'-Python

分類Dev

AttributeError( "'str' object has no attribute 'read'")

分類Dev

AttributeError: 'module' object(scipy)has no attribute 'misc'

分類Dev

AttributeError: 'module' object(scipy)has no attribute 'misc'

分類Dev

AttributeError: 'DeferredAttribute' object has no attribute '_meta'

分類Dev

AttributeError( "'str' object has no attribute 'read'")

分類Dev

AttributeError( "'str' object has no attribute 'read'")

分類Dev

AttributeError: 'Snackbar' object has no attribute 'show'

分類Dev

AttributeError: 'module' object has no attribute 'urlretrieve'

分類Dev

AttributeError: 'ElementTree' object has no attribute 'tag' in Python

分類Dev

AttributeError: 'DeferredAttribute' object has no attribute 'rel'

分類Dev

AttributeError: 'DataFrame' object has no attribute 'dtype'

分類Dev

AttributeError: 'Tensor' object has no attribute 'compile'

分類Dev

AttributeError: 'MyGrid' object has no attribute for my function

分類Dev

AttributeError: 'QScatterSeries' object has no attribute 'points'

分類Dev

AttributeError: 'Timestamp' object has no attribute 'translate'

分類Dev

AttributeError: 'list' object has no attribute 'time'

分類Dev

AttributeError: 'list' object has no attribute 'sample'

分類Dev

AttributeError: 'NoneType' object has no attribute 'upper'

分類Dev

AttributeError: 'tuple' object has no attribute 'read'

Related 関連記事

  1. 1

    Dask compute gives AttributeError: 'Series' object has no attribute 'encode'

  2. 2

    AttributeError: 'module' object has no attribute

  3. 3

    AttributeError: 'function' object has no attribute

  4. 4

    AttributeError: 'bool' object has no attribute

  5. 5

    SQLAlchemy "AttributeError: 'str' object has no attribute 'c'"

  6. 6

    TensorFlow:AttributeError: 'Tensor' object has no attribute 'shape'

  7. 7

    AttributeError: 'module' object has no attribute 'computation'

  8. 8

    Scrapy:AttributeError: 'list' object has no attribute 'iteritems'

  9. 9

    AttributeError: 'module' object has no attribute 'reader'

  10. 10

    AttributeError: 'list' object has no attribute 'split'-Python

  11. 11

    AttributeError( "'str' object has no attribute 'read'")

  12. 12

    AttributeError: 'module' object(scipy)has no attribute 'misc'

  13. 13

    AttributeError: 'module' object(scipy)has no attribute 'misc'

  14. 14

    AttributeError: 'DeferredAttribute' object has no attribute '_meta'

  15. 15

    AttributeError( "'str' object has no attribute 'read'")

  16. 16

    AttributeError( "'str' object has no attribute 'read'")

  17. 17

    AttributeError: 'Snackbar' object has no attribute 'show'

  18. 18

    AttributeError: 'module' object has no attribute 'urlretrieve'

  19. 19

    AttributeError: 'ElementTree' object has no attribute 'tag' in Python

  20. 20

    AttributeError: 'DeferredAttribute' object has no attribute 'rel'

  21. 21

    AttributeError: 'DataFrame' object has no attribute 'dtype'

  22. 22

    AttributeError: 'Tensor' object has no attribute 'compile'

  23. 23

    AttributeError: 'MyGrid' object has no attribute for my function

  24. 24

    AttributeError: 'QScatterSeries' object has no attribute 'points'

  25. 25

    AttributeError: 'Timestamp' object has no attribute 'translate'

  26. 26

    AttributeError: 'list' object has no attribute 'time'

  27. 27

    AttributeError: 'list' object has no attribute 'sample'

  28. 28

    AttributeError: 'NoneType' object has no attribute 'upper'

  29. 29

    AttributeError: 'tuple' object has no attribute 'read'

ホットタグ

アーカイブ