BUG: pandas.cut and negative values #14652

Closed
luca-s opened this Issue Nov 14, 2016 · 3 comments

Comments

Projects
None yet
2 participants
Contributor

luca-s commented Nov 14, 2016 edited

Here is an example of pandas.cut ran on a pandas.Series with only one positive element and then on a pandas.Series with only one negative element. In the second scenario pandas.cut is not able to insert the single value on the only one bin.

I might be wrong but I expected pandas.cut to behave on negative values the same as with positive
values.

A small, complete example of the issue

import pandas as pd
import numpy as np

>>> s = pd.Series([1.])
>>> pd.cut(s, 1)
0    (0.999, 1.001]
dtype: category
Categories (1, object): [(0.999, 1.001]]

>>> s = pd.Series([-1.])
>>> pd.cut(s, 1)
0    NaN
dtype: category
Categories (1, object): [(-0.999, -1.001]]

Expected Output

>>> s = pd.Series([-1.])
>>> pd.cut(s, 1)

0    (-1.001, -0.999]
dtype: category
Categories (1, object): [(-1.001, -0.999]]

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.4.3.final.0 python-bits: 64 OS: Linux OS-release: 4.4.0-45-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8

pandas: 0.18.1
nose: 1.3.1
pip: 1.5.4
setuptools: 28.0.0
Cython: 0.20.1post0
numpy: 1.11.1
scipy: 0.18.0
statsmodels: 0.6.1
xarray: None
IPython: 5.1.0
sphinx: 1.4.8
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.6.1
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.5.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999
httplib2: 0.8
apiclient: None
sqlalchemy: 1.0.14
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None
pandas_datareader: 0.2.1

Contributor

jreback commented Nov 15, 2016

hmm, that does look wrong.

welcome for you to have a look!

jreback added this to the Next Major Release milestone Nov 15, 2016

Contributor

luca-s commented Nov 15, 2016 edited

Ok, I had a look and the problem is probably in here, the code should be:
mn -= .001 * abs(mn)
and also here, the code should be:
mx += .001 * abs(mx)

Should I submit a PR? I am not familiar with Pandas development

Contributor

jreback commented Nov 15, 2016

@luca-s perfect. if you'd like to do a PR would be great.

jreback changed the title from BUG (?) pandas.cut and negative bins to BUG: pandas.cut and negative values Nov 15, 2016

@luca-s luca-s added a commit to luca-s/pandas that referenced this issue Nov 15, 2016

@luca-s luca-s BUG: pandas.cut and negative values #14652
Fix issue #14652
2db0c7a

@luca-s luca-s added a commit to luca-s/pandas that referenced this issue Nov 16, 2016

@luca-s luca-s Added test case for #14652 fdc55b9

@jreback jreback modified the milestone: 0.19.2, Next Major Release Nov 17, 2016

jreback closed this in 2fc0c68 Nov 17, 2016

@amolkahat amolkahat added a commit to amolkahat/pandas that referenced this issue Nov 26, 2016

@luca-s @amolkahat luca-s + amolkahat BUG: pandas.cut and negative values #14652
closes #14652

Author: Luca Scarabello <luca.scarabello@sed.ethz.ch>
Author: Luca <luca.scarabello@gmail.com>

Closes #14663 from luca-s/issue_14652 and squashes the following commits:

8db26db [Luca Scarabello] Moved new test location to pandas\tools\tests\test_tile.py
90dd07d [Luca Scarabello] Updated whatsnew
d6b3da8 [Luca Scarabello] fixed flake8 compliance
fdc55b9 [Luca Scarabello] Added test case for #14652
d5790e2 [Luca Scarabello] updated whatsnew v0.19.2
2db0c7a [Luca] BUG: pandas.cut and negative values #14652
483fc79

@jorisvandenbossche jorisvandenbossche added a commit to jorisvandenbossche/pandas that referenced this issue Dec 14, 2016

@luca-s @jorisvandenbossche luca-s + jorisvandenbossche [Backport #14663] BUG: pandas.cut and negative values #14652
closes #14652

Author: Luca Scarabello <luca.scarabello@sed.ethz.ch>
Author: Luca <luca.scarabello@gmail.com>

Closes #14663 from luca-s/issue_14652 and squashes the following commits:

8db26db [Luca Scarabello] Moved new test location to pandas\tools\tests\test_tile.py
90dd07d [Luca Scarabello] Updated whatsnew
d6b3da8 [Luca Scarabello] fixed flake8 compliance
fdc55b9 [Luca Scarabello] Added test case for #14652
d5790e2 [Luca Scarabello] updated whatsnew v0.19.2
2db0c7a [Luca] BUG: pandas.cut and negative values #14652

(cherry picked from commit 2fc0c68)
8e31489
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment