Skip to content

BUG: min/max of empty datetime dataframe raises #33704

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

Closed
2 of 3 tasks
adbull opened this issue Apr 21, 2020 · 1 comment · Fixed by #33911
Closed
2 of 3 tasks

BUG: min/max of empty datetime dataframe raises #33704

adbull opened this issue Apr 21, 2020 · 1 comment · Fixed by #33911
Labels
Milestone

Comments

@adbull
Copy link
Contributor

adbull commented Apr 21, 2020

  • 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.

Code Sample, a copy-pastable example

import pandas as pd
df = pd.DataFrame(dict(x=pd.to_datetime([])))
df.max()
Traceback (most recent call last):
  File "<ipython-input-17-be9940feb663>", line 1, in <module>
    df.max()
  File "pandas/core/generic.py", line 11215, in stat_func
    f, name, axis=axis, skipna=skipna, numeric_only=numeric_only
  File "pandas/core/frame.py", line 7907, in _reduce
    result = f(values)
  File "pandas/core/frame.py", line 7865, in f
    return op(x, axis=axis, skipna=skipna, **kwds)
  File "pandas/core/nanops.py", line 109, in f
    return _na_for_min_count(values, axis)
  File "pandas/core/nanops.py", line 392, in _na_for_min_count
    result.fill(fill_value)
ValueError: cannot convert float NaN to integer

Problem description

When taking the min/max of an empty datetime dataframe, a ValueError is raised. This is surprising, and inconsistent with the case of an empty datetime series, where min/max return NaT.

Expected Output

x   NaT
dtype: datetime64[ns]

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.7.final.0
python-bits : 64
OS : Linux
OS-release : 4.20.11-100.fc28.x86_64
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 1.0.3
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.1.3.post20200330
Cython : 0.29.15
pytest : 5.4.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.1
IPython : 7.13.0
pandas_datareader: None
bs4 : 4.9.0
bottleneck : 1.3.2
fastparquet : None
gcsfs : None
lxml.etree : 4.5.0
matplotlib : 3.1.3
numexpr : 2.7.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 0.15.1
pytables : None
pytest : 5.4.1
pyxlsb : None
s3fs : None
scipy : 1.2.1
sqlalchemy : 1.3.16
tables : 3.6.1
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@adbull adbull added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 21, 2020
@CloseChoice
Copy link
Member

CloseChoice commented Apr 29, 2020

Note the difference here:

In [1]: import pandas as pd                                                                                                                      
# initialize a series from pd.to_datetime
In [2]: srs = pd.Series(pd.to_datetime([]))                                                                                                      

In [3]: srs                                                                                                                                      
Out[3]: Series([], dtype: datetime64[ns])

In [4]: srs.max()                                                                                                                                
Out[4]: NaT

In [5]: df1 = pd.DataFrame(dict(x=pd.to_datetime([])))                                                                                           

In [6]: df1                                                                                                                                      
Out[6]: 
Empty DataFrame
Columns: [x]
Index: []
...
In [8]: df1.max()                                                                                                                                
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-ffa8d8284b6e> in <module>
----> 1 df1.max()

~/programming/python/pandas_tests/pandas/pandas/core/generic.py in stat_func(self, axis, skipna, level, numeric_only, **kwargs)
  11172             return self._agg_by_level(name, axis=axis, level=level, skipna=skipna)
  11173         #import pdb; pdb.set_trace()()
> 11174         return self._reduce(
  11175             func, name=name, axis=axis, skipna=skipna, numeric_only=numeric_only
  11176         )

~/programming/python/pandas_tests/pandas/pandas/core/frame.py in _reduce(self, op, name, axis, skipna, numeric_only, filter_type, **kwds)
   8377 
   8378             try:
-> 8379                 result = f(values)
   8380 
   8381             except TypeError:

~/programming/python/pandas_tests/pandas/pandas/core/frame.py in f(x)
   8296 
   8297         def f(x):
-> 8298             return op(x, axis=axis, skipna=skipna, **kwds)
   8299 
   8300         def _get_data(axis_matters):

~/programming/python/pandas_tests/pandas/pandas/core/nanops.py in f(values, axis, skipna, **kwds)
    111                 # correctly handle empty inputs and remove this check.
    112                 # It *may* just be `var`
--> 113                 return _na_for_min_count(values, axis)
    114 
    115             if _USE_BOTTLENECK and skipna and _bn_ok_dtype(values.dtype, bn_name):

~/programming/python/pandas_tests/pandas/pandas/core/nanops.py in _na_for_min_count(values, axis)
    386         result_shape = values.shape[:axis] + values.shape[axis + 1 :]
    387         result = np.empty(result_shape, dtype=values.dtype)
--> 388         result.fill(fill_value)
    389         return result
    390 

ValueError: cannot convert float NaN to integer

# initializing dataframe with an empty list of pandas datetimes
In [9]: df2 = pd.DataFrame([pd.to_datetime([])])                                                                                                 

In [10]: df2                                                                                                                                     
Out[10]: 
Empty DataFrame
Columns: []
Index: [0]

In [11]: df2.max()                                                                                                                               
Out[11]: Series([], dtype: float64)

# when only column x is taken
In [13]: df1.x.max()                                                                                                                             
Out[13]: NaT

This totally seems unexpected, especially [13].

@jreback jreback added this to the 1.1 milestone May 9, 2020
@bashtage bashtage removed the Needs Triage Issue that has not been reviewed by a pandas team member label Aug 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants