Skip to content

pd.Series.diff() on boolean values #17294

Closed
@mrtarsa

Description

@mrtarsa

Code Sample

>> x = pd.Series([True,False,True])
>> x.diff()
0     NaN
1    True
2    True
dtype: object
>> x - x.shift()
0    NaN
1     -1
2      1
dtype: object

Problem description

It's counter-intuitive that the results of above are different.

The current implementation of pd.Series.diff uses algorithms.diff that subtracts 2 numpy arrays in the following way

out_arr[res_indexer] = arr[res_indexer] - arr[lag_indexer]

As pointed here such behaviour is deprecated in favor to np.diff. But np.diff also treats booleans in binary operations in its own numpy way, that is different from native python (replace False with 0, replace True with 1).

>> np.array([True, False]) - np.array([False, True])
/home/deoxys/miniconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: DeprecationWarning: numpy boolean subtract, the `-` operator, is deprecated, use the bitwise_xor, the `^` operator, or the logical_xor function instead.
array([ True,  True], dtype=bool)
>> np.diff([False, True, False])
array([ True,  True], dtype=bool)
>> True - False
1
>> False - True
-1

Expected Output

I believe there is no correct way of subtracting booleans. But, it's definitely strange that operations like x - x.shift() and x.diff() provide different results.

Output of pd.show_versions()

INSTALLED VERSIONS ------------------

commit: None
python: 3.6.0.final.0
python-bits: 64
OS: Linux
OS-release: 4.11.9-1-ARCH
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.20.1
pytest: None
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.12.1
scipy: 0.19.0
xarray: None
IPython: 6.0.0
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.0.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.6.0
html5lib: 0.999
sqlalchemy: None
pymysql: None
psycopg2: 2.7.1 (dt dec pq3 ext lo64)
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions