3

I have below pandas data frame

import pandas as pd
import numpy as np
dat = pd.DataFrame({'A' : [1,2,3,4,5], 'B' : ['2002-01-01', '2003-01-01', '2004-01-01', '2004-01-01', '2005-01-01']})
dat['A'] = dat['A'].astype('Int64')

Now I want to create another date column from column B by adding number of months from column A. Below is my code

pd.to_datetime(dat['B'], errors = 'coerce', dayfirst = True) + pd.DateOffset(months = dat['A'])

However with that, I get below error,

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "offsets.pyx", line 1382, in pandas._libs.tslibs.offsets.RelativeDeltaOffset.__init__
  File "offsets.pyx", line 328, in pandas._libs.tslibs.offsets._determine_offset
  File "/Users/abc/Python_VENV/lib/python3.12/site-packages/dateutil/relativedelta.py", line 172, in __init__
    if any(x is not None and x != int(x) for x in (years, months)):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/abc/Python_VENV/lib/python3.12/site-packages/dateutil/relativedelta.py", line 172, in <genexpr>
    if any(x is not None and x != int(x) for x in (years, months)):
                                  ^^^^^^
  File "/Users/abc/Python_VENV/lib/python3.12/site-packages/pandas/core/series.py", line 248, in wrapper
    raise TypeError(f"cannot convert the series to {converter}")
TypeError: cannot convert the series to <class 'int'>

Could you please help to resolve this?

1

1 Answer 1

2

I think the simplest way to solve this problem is to use a loop.

  1. Using apply
dat['B'] = pd.to_datetime(dat['B'], errors = 'coerce', dayfirst = True)
dat['C'] = dat.apply(lambda x: x['B'] + pd.DateOffset(months = x['A']), axis=1)

dat

        A   B           C
0   1   2002-01-01  2002-02-01
1   2   2003-01-01  2003-03-01
2   3   2004-01-01  2004-04-01
3   4   2004-01-01  2004-05-01
4   5   2005-01-01  2005-06-01
  1. Using list comprehension
dat['B'] = pd.to_datetime(dat['B'], errors = 'coerce', dayfirst = True)
dat['C'] = [b + pd.DateOffset(months=a) for a, b in zip(dat['A'], dat['B'])]

same result.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. This works perfectly, but fairly slow for a large data frame
Yeah. That’s the limitation of loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.