Skip to content

Maximum of time series with NaT returns a dtype float64 with only NaN's #28868

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
MaximeWeyl opened this issue Oct 9, 2019 · 2 comments
Closed

Comments

@MaximeWeyl
Copy link

Code Sample, a copy-pastable example if possible

import pandas as pd

show_bug = True

a = pd.date_range("2019-1-1", periods=10, freq="1D", tz="Europe/Paris")
b = pd.date_range("2019-1-3", periods=10, freq="1H", tz="Europe/Paris")

if show_bug:
    a = pd.Index([pd.NaT] + a.to_list()[1:])



multi = pd.MultiIndex.from_arrays([a, b])
m = multi.to_frame(index=False).max(axis=1)
print(m)

Problem description

When show_bug is False, no NaT's are present. This gives this output :


0   2019-01-03 00:00:00+01:00
1   2019-01-03 01:00:00+01:00
2   2019-01-03 02:00:00+01:00
3   2019-01-04 00:00:00+01:00
4   2019-01-05 00:00:00+01:00
5   2019-01-06 00:00:00+01:00
6   2019-01-07 00:00:00+01:00
7   2019-01-08 00:00:00+01:00
8   2019-01-09 00:00:00+01:00
9   2019-01-10 00:00:00+01:00
dtype: datetime64[ns, Europe/Paris]


When show_bug is True, all it does is replacing the first value in a with a NaT.
I expect the output to be the same, except for the first row being a NaT.

Here is what I get for show_bug=True :


0   NaN
1   NaN
2   NaN
3   NaN
4   NaN
5   NaN
6   NaN
7   NaN
8   NaN
9   NaN
dtype: float64


Expected Output


0   NaT
1   2019-01-03 01:00:00+01:00
2   2019-01-03 02:00:00+01:00
3   2019-01-04 00:00:00+01:00
4   2019-01-05 00:00:00+01:00
5   2019-01-06 00:00:00+01:00
6   2019-01-07 00:00:00+01:00
7   2019-01-08 00:00:00+01:00
8   2019-01-09 00:00:00+01:00
9   2019-01-10 00:00:00+01:00
dtype: datetime64[ns, Europe/Paris]

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.3.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 9, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None

pandas : 0.25.1
numpy : 1.17.0
pytz : 2019.2
dateutil : 2.8.0
pip : 19.0.3
setuptools : 40.8.0
Cython : None
pytest : 4.6.5
hypothesis : 4.33.1
sphinx : 2.2.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.4.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.1
IPython : 7.7.0
pandas_datareader: None
bs4 : 4.8.0
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.4.1
matplotlib : 3.1.1
numexpr : None
odfpy : None
openpyxl : 2.6.3
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : 1.3.1
sqlalchemy : None
tables : None
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : None

@MaximeWeyl
Copy link
Author

Here is a workaround I intend to use while this bug exists :

import pandas as pd

insert_nat = True

def column_max_without_nans(df):
    return df[df.notnull().all(axis=1)].max(axis=1).reindex(df.index)


a = pd.date_range("2019-1-1", periods=10, freq="1D", tz="Europe/Paris")
b = pd.date_range("2019-1-3", periods=10, freq="1H", tz="Europe/Paris")

if insert_nat:
    a = pd.Index([pd.NaT] + a.to_list()[1:])



multi = pd.MultiIndex.from_arrays([a, b])
m = column_max_without_nans(multi.to_frame(index=False))
print(m)

Wich gives me what I want :


0                         NaT
1   2019-01-03 01:00:00+01:00
2   2019-01-03 02:00:00+01:00
3   2019-01-04 00:00:00+01:00
4   2019-01-05 00:00:00+01:00
5   2019-01-06 00:00:00+01:00
6   2019-01-07 00:00:00+01:00
7   2019-01-08 00:00:00+01:00
8   2019-01-09 00:00:00+01:00
9   2019-01-10 00:00:00+01:00
dtype: datetime64[ns, Europe/Paris]

@mroeschke
Copy link
Member

Duplicate of #27794. Investigations and PR's welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants