-
- Notifications
You must be signed in to change notification settings - Fork 19.4k
Closed
Labels
BugError ReportingIncorrect or improved errors from pandasIncorrect or improved errors from pandasTransformationse.g. cumsum, diff, ranke.g. cumsum, diff, rankgood first issue
Description
Pandas version checks
-
I have checked that this issue has not already been reported.
-
I have confirmed this bug exists on the latest version of pandas.
-
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
In [1]: import pandas as pd df = In [2]: df = pd.DataFrame() In [3]: df.diff(0.5) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-3-03698d8a038c> in ?() ----> 1 df.diff(0.5) ~/.miniconda3/envs/snowpark/lib/python3.8/site-packages/pandas/core/frame.py in ?(self, periods, axis) 9216 is_float(periods) 9217 # error: "int" has no attribute "is_integer" 9218 and periods.is_integer() # type: ignore[attr-defined] 9219 ): -> 9220 raise ValueError("periods must be an integer") 9221 periods = int(periods) 9222 9223 axis = self._get_axis_number(axis) ValueError: periods must be an integer In [4]: series = pd.Series() <ipython-input-4-2b0f0f5d39e8>:1: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning. series = pd.Series() In [5]: series.diff(0.5) Out[5]: Series([], dtype: float64) In [6]: df.diff('1') --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-6-ffa50df92031> in ?() ----> 1 df.diff('1') ~/.miniconda3/envs/snowpark/lib/python3.8/site-packages/pandas/core/frame.py in ?(self, periods, axis) 9216 is_float(periods) 9217 # error: "int" has no attribute "is_integer" 9218 and periods.is_integer() # type: ignore[attr-defined] 9219 ): -> 9220 raise ValueError("periods must be an integer") 9221 periods = int(periods) 9222 9223 axis = self._get_axis_number(axis) ValueError: periods must be an integer In [7]: series.diff('1') Out[7]: Series([], dtype: float64) In [8]: series.diff('a') --------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[8], line 1 ----> 1 series.diff('a') File ~/.miniconda3/envs/snowpark/lib/python3.8/site-packages/pandas/core/series.py:2903, in Series.diff(self, periods) 2818 @doc( 2819 klass="Series", 2820 extra_params="", (...) 2866 ) 2867 def diff(self, periods: int = 1) -> Series: 2868 """ 2869 First discrete difference of element. 2870 (...) 2901 {examples} 2902 """ -> 2903 result = algorithms.diff(self._values, periods) 2904 return self._constructor(result, index=self.index).__finalize__( 2905 self, method="diff" 2906 ) File ~/.miniconda3/envs/snowpark/lib/python3.8/site-packages/pandas/core/algorithms.py:1699, in diff(arr, n, axis) 1679 def diff(arr, n: int, axis: int = 0): 1680 """ 1681 difference of n between self, 1682 analogous to s-s.shift(n) (...) 1696 shifted 1697 """ -> 1699 n = int(n) 1700 na = np.nan 1701 dtype = arr.dtype ValueError: invalid literal for int() with base 10: 'a' ### Issue Description Series.diff doesn't validate the `periods` argument, while `df.diff` does. ### Expected Behavior I think that Series.diff should also validate and throw the same exceptions as df.diff. ### Installed Versions <details> INSTALLED VERSIONS ------------------ commit : 2e218d10984e9919f0296931d92ea851c6a6faf5 python : 3.8.18.final.0 python-bits : 64 OS : Darwin OS-release : 23.2.0 Version : Darwin Kernel Version 23.2.0: Wed Nov 15 21:55:06 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T6020 machine : x86_64 processor : i386 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8 pandas : 1.5.3 numpy : 1.24.3 pytz : 2023.3.post1 dateutil : 2.8.2 setuptools : 68.0.0 pip : 23.2.1 Cython : None pytest : 7.4.3 hypothesis : None sphinx : 5.0.2 blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : 3.1.2 IPython : 8.12.3 pandas_datareader: None bs4 : 4.12.2 bottleneck : 1.3.5 brotli : None fastparquet : None fsspec : None gcsfs : None matplotlib : 3.7.3 numba : None numexpr : 2.8.4 odfpy : None openpyxl : None pandas_gbq : None pyarrow : 10.0.1 pyreadstat : None pyxlsb : None s3fs : None scipy : None snappy : None sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None xlwt : None zstandard : None tzdata : 2023.3 </details> Metadata
Metadata
Assignees
Labels
BugError ReportingIncorrect or improved errors from pandasIncorrect or improved errors from pandasTransformationse.g. cumsum, diff, ranke.g. cumsum, diff, rankgood first issue