wesm / pydata-book

Materials and IPython notebooks for "Python for Data Analysis" by Wes McKinney, published by O'Reilly Media
Other
22.27k stars 15.19k forks source link

Ch05 adding obj3 + 4 - don't understand resulting value for "Utah" #144

Closed inanoll2 closed 3 years ago

inanoll2 commented 3 years ago

I have a question: when adding both obj 3 + 4, the result shows NaN for "Utah". However, in obj3, its value is 5000.

Why does it add values for Ohio, Oregon and Texas, but not for Utah? I would have expected it to show 5000.

Thanks for helping out.

wesm commented 3 years ago

Since 'Utah' is not present in obj4, is it treated as missing and so yields missing (null / NaN) in the result. You can get around this by using the add method:

>>> obj3.add(obj4, fill_value=0)
California         NaN
Ohio           70000.0
Oregon         32000.0
Texas         142000.0
Utah            5000.0
dtype: float64

This treats missing index values in obj3 or obj4 as 0