BUG: Change of behavior in casting of datetime-like types in MultiIndex
See original GitHub issue-
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
[Edited to inform a much simpler example.]
import datetime
import pandas as pd
print(f"Pandas version:\t{pd.__version__}\n")
df = pd.DataFrame({'date': [datetime.date(2021, 8, 1),
datetime.date(2021, 8, 2),
datetime.date(2021, 8, 3)],
'ticker': ['aapl', 'goog', 'yhoo'],
'value': [5.63269, 4.45609, 2.74843]})
df.set_index(['date', 'ticker'], inplace=True)
print(df.index.get_level_values(0))
Output
The output below has been generated with pandas 1.3.0 or higher.
Pandas version: 1.3.0
Index([2021-08-01, 2021-08-02, 2021-08-03], dtype='object', name='date')
Expected Output
The output below has been generated with pandas 1.2.5.
Pandas version: 1.2.5
DatetimeIndex(['2021-08-01', '2021-08-02', '2021-08-03'], dtype='datetime64[ns]', name='date', freq=None)
Problem description
Starting from pandas 1.3.0, the observed behavior changed: in a MultiIndex
creation, datetime.date
objects are not cast to datetime64
anymore. I fail to find in the What’s new page the reason for that change of behavior. Is it by design or a bug?
Output of pd.show_versions()
INSTALLED VERSIONS
------------------
commit : 5f648bf1706dd75a9ca0d29f26eadfbb595fe52b
python : 3.9.6.final.0
python-bits : 64
OS : Darwin
OS-release : 19.6.0
Version : Darwin Kernel Version 19.6.0: Tue Jun 22 19:49:55 PDT 2021; root:xnu-6153.141.35~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.3.2
numpy : 1.21.2
pytz : 2021.1
dateutil : 2.8.2
pip : 21.2.4
setuptools : 57.4.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.9.1 (dt dec pq3 ext lo64)
jinja2 : None
IPython : 7.26.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : 1.4.22
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None
Issue Analytics
- State:
- Created 2 years ago
- Comments:17 (11 by maintainers)
Top Results From Across the Web
BUG: incorrect type when casting to nullable type in multiindex ...
When casting to a nullable type (like "Int64") in a multiindex column dataframe, the resulting type is "object" (where it should be "Int64")....
Read more >What's new in 1.3.0 (July 2, 2021) - Pandas
Improved integer type mapping from pandas to SQLAlchemy when using DataFrame.to_sql() ... These are bug fixes that might have notable behavior changes.
Read more >python-pandas-0.23.4-bp151.2.3 - SUSE Package Hub -
Update to 0.23.4 * Python 3.7 with Windows gave all missing values for ... with a MultiIndex or multiple keys that contains categorical...
Read more >pandas: convert index type in multiindex dataframe
How do I convert the inner values of the df's index to str? My attempt: df1.index.astype(str). returns a TypeError.
Read more >pandas documentação - Python - 15 - Passei Direto
when there is a time change (DST) and resampling frequecy is 12h or higher (GH15549) • Bug in pd.DataFrameGroupBy.count() when counting over a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’d go for
lib.infer_dtype(col, skipna=True) == "date"
instead of checking for “mixed”It’s possible. Though we’d then have a breaking change for anyone relying on the 1.3 behavior.
I’d check
Index(col).inferred_type == "date"