Search code examples
pythonpandasdataframesubtraction

Subtracting two pandas series with same index is giving NaN values


I have two series, both of which have datetime.time indices from 00:00 - 00:05. There is 1 missing values in the first series, which ideally should appear NaN after subtraction operation is performed. However, the entire result is NaN. I am using series.sub to perform this subtraction.

But this operation is giving NaN values for all columns.

print(s8)
time
00:00:00    12.697916
00:01:00    12.062659
00:02:00    11.956684
00:04:00    12.818977
00:05:00    12.309423

print(f8) 
time
00:00:00    14.551911
00:01:00    14.392418
00:02:00    14.488430
00:03:00    14.542103
00:04:00    14.397826  

delta = f8.sub(s8)
print(delta)  

time
00:00:00   NaN
00:01:00   NaN
00:02:00   NaN
00:03:00   NaN
00:04:00   NaN
00:05:00   NaN

Ideally it should be,

time
00:00:00   2.1
00:01:00   2.0
00:02:00   1.5
00:03:00   1.2
00:04:00   NaN
00:05:00   NaN

The ideal values above are just estimate of the subtraction. Please advise how this can b fixed.


Solution

  • Solution based on the comments:

    Check the column dtypes. As in your case, they were string instead of a numeric one.