-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
pd.Series.diff() on boolean values #17294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@tarashypka so easiest is to raise on all boolean .shift/.diff operations. Or is there a correct method here? |
@jreback My suggestion that in this case it will be correct for
to return
that will be consistent with the way numpy treats booleans or raise an error/deprecation warning as numpy does. In order to get desired behaviour user may want to clarify his intentions to treat booleans as 0, 1 with
or
that will lead to
Also it may be helpful to raise a warning when user subtracts booleans. |
Doing:
With
|
Code Sample
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
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).
Expected Output
I believe there is no correct way of subtracting booleans. But, it's definitely strange that operations like
x - x.shift()
andx.diff()
provide different results.Output of
pd.show_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
The text was updated successfully, but these errors were encountered: