Skip to content

BUG: .diff() not working on int8 and int16 dtype #39050

Closed
@nrcjea001

Description

@nrcjea001
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


# Your code here
df=pd.DataFrame({'a':[1,2,3,3,2],'b':[0,1,1,0,0]})
df['b']=df['b'].astype(np.int8)
df.groupby('a')['b'].diff()

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-23-f6026edf0092> in <module>
      1 df=pd.DataFrame({'a':[1,2,3,3,2],'b':[0,1,1,0,0]})
      2 df['b']=df['b'].astype(np.int8)
----> 3 df.groupby('a')['b'].diff()

~\Anaconda3\lib\site-packages\pandas\core\groupby\groupby.py in wrapper(*args, **kwargs)
    815                 return self.apply(curried)
    816 
--> 817             return self._python_apply_general(curried, self._obj_with_exclusions)
    818 
    819         wrapper.__name__ = name

~\Anaconda3\lib\site-packages\pandas\core\groupby\groupby.py in _python_apply_general(self, f, data)
    926             data after applying f
    927         """
--> 928         keys, values, mutated = self.grouper.apply(f, data, self.axis)
    929 
    930         return self._wrap_applied_output(

~\Anaconda3\lib\site-packages\pandas\core\groupby\ops.py in apply(self, f, data, axis)
    235             # group might be modified
    236             group_axes = group.axes
--> 237             res = f(group)
    238             if not _is_indexed_like(res, group_axes, axis):
    239                 mutated = True

~\Anaconda3\lib\site-packages\pandas\core\groupby\groupby.py in curried(x)
    804 
    805             def curried(x):
--> 806                 return f(x, *args, **kwargs)
    807 
    808             # preserve the name so we can detect it when calling plot methods,

~\Anaconda3\lib\site-packages\pandas\core\series.py in diff(self, periods)
   2436         {examples}
   2437         """
-> 2438         result = algorithms.diff(self.array, periods)
   2439         return self._constructor(result, index=self.index).__finalize__(
   2440             self, method="diff"

~\Anaconda3\lib\site-packages\pandas\core\algorithms.py in diff(arr, n, axis, stacklevel)
   2000         # TODO: can diff_2d dtype specialization troubles be fixed by defining
   2001         #  out_arr inside diff_2d?
-> 2002         algos.diff_2d(arr, out_arr, n, axis, datetimelike=is_timedelta)
   2003     else:
   2004         # To keep mypy happy, _res_indexer is a list while res_indexer is

pandas\_libs\algos.pyx in pandas._libs.algos.diff_2d()

NotImplementedError: 

Problem description

I try to take the difference of dataframe on columns with dtype int8 and int16 but this yields an error. This was working in previous version of pandas, 1.1.5, but not in current version, 1.2.0.

Expected Output

0    NaN
1    NaN
2    NaN
3   -1.0
4   -1.0

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 3e89b4c
python : 3.8.6.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19041
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 11, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 1.2.0
numpy : 1.19.5
pytz : 2020.5
dateutil : 2.8.1
pip : 20.3.3
setuptools : 49.6.0.post20210108
Cython : 0.29.21
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.2
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.19.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : 0.8.5
fastparquet : None
gcsfs : None
matplotlib : 3.3.3
numexpr : 2.7.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 2.0.0
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : None
tables : 3.6.1
tabulate : 0.8.7
xarray : 0.16.2
xlrd : None
xlwt : None
numba : 0.52.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    AlgosNon-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diffDtype ConversionsUnexpected or buggy dtype conversionsRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions