Home > other >  Python: PerformanceWarning: Adding/subtracting object-dtype array to TimedeltaArray not vectorized
Python: PerformanceWarning: Adding/subtracting object-dtype array to TimedeltaArray not vectorized

Time:10-21

I have a data frame DF with 2 date columns (object data type) in the below format:

date_column1 date_column2
2021-08-14    2021-09-19
2021-10-11    2021-11-02
2019-09-15    2021-10-25
...           ...

and I want to insert a new column based the below:

DF['date_new'] = DF['date_column2']   (DF['date_column2']-DF['date_column1'])

The code works but I am getting the below warning

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\arrays\datetimelike.py:1190: 
PerformanceWarning: Adding/subtracting object-dtype array to TimedeltaArray not vectorized

How should I adapt my code to fix this warning?

CodePudding user response:

Simulated with your sample data, the situation happens when your date_column1 is of string type while date_column2 is of datetime type. Other combinations give other kind of errors. When both are in datetime type, there is no error.

You will get through the warning by converting date_column1 also to datetime type, as follows:

DF['date_column1'] = pd.to_datetime(DF['date_column1'])

# or to play safe, also convert `date_column2`
DF['date_column2'] = pd.to_datetime(DF['date_column2'])

After that, you will get successful result as follows without warning:

0   2021-10-25
1   2021-11-24
2   2023-12-05
dtype: datetime64[ns]
  • Related