Skip to content

BUG: Concatenating data frames with MultiIndex with datetime64[ms] dtype introduces NaT values to the index #61342

@shchur

Description

@shchur

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

import pandas as pd

def resample_each_item(dtype) -> pd.DataFrame:
    df = pd.DataFrame(
        [
            ["A", "2023-01-15", 42],
            ["A", "2023-01-17", 33],
            ["B", "2023-02-20", 78],
            ["B", "2023-02-23", 91],
        ],
        columns=["item_id", "timestamp", "target"],
    )
    df["timestamp"] = pd.to_datetime(df["timestamp"]).astype(dtype)
    df = df.set_index(["item_id", "timestamp"])
    resampled = []
    for item_id in ["A", "B"]:
        resampled.append(pd.concat({item_id: df.loc[item_id].resample("D", level="timestamp").mean()}))
    return pd.concat(resampled)

print(resample_each_item("datetime64[ns]"))
# For datetime64[ns] all timestamps are valid
#               target
#   timestamp         
# A 2023-01-15    42.0
#   2023-01-16     NaN
#   2023-01-17    33.0
# B 2023-02-20    78.0
#   2023-02-21     NaN
#   2023-02-22     NaN
#   2023-02-23    91.0

print(resample_each_item("datetime64[ms]"))
# For datetime64[ms] or datetime64[s] dtypes, NaT values are introduced
#               target
#   timestamp         
# A 2023-01-15    42.0
#   NaT            NaN
#   NaT           33.0
# B 2023-02-20    78.0
#   NaT            NaN
#   NaT            NaN
#   NaT           91.0

Issue Description

When concatenating data frames with MultiIndex, where one level is of type datetime64[ms] or datetime64[s], some timestamps are replaced with NaT. If the timestamps are of dtype datetime64[ns], no NaT values are introduced.

Expected Behavior

No NaT values are introduced, regardless of whether the timestamp dtype is datetime64[ms], datetime64[s] or datetime64[ns].

Installed Versions

INSTALLED VERSIONS
------------------
commit                : 0691c5cf90477d3503834d983f69350f250a6ff7
python                : 3.12.9
python-bits           : 64
OS                    : Linux
OS-release            : 6.1.132-147.221.amzn2023.x86_64
Version               : #1 SMP PREEMPT_DYNAMIC Tue Apr  8 13:14:54 UTC 2025
machine               : x86_64
processor             : x86_64
byteorder             : little
LC_ALL                : None
LANG                  : C.UTF-8
LOCALE                : C.UTF-8

pandas                : 2.2.3
numpy                 : 1.26.4
pytz                  : 2025.2
dateutil              : 2.9.0.post0
pip                   : 25.0.1
Cython                : None
sphinx                : None
IPython               : 8.12.3
adbc-driver-postgresql: None
adbc-driver-sqlite    : None
bs4                   : 4.13.3
blosc                 : None
bottleneck            : None
dataframe-api-compat  : None
fastparquet           : None
fsspec                : 2024.12.0
html5lib              : None
hypothesis            : None
gcsfs                 : None
jinja2                : 3.1.6
lxml.etree            : None
matplotlib            : 3.10.1
numba                 : 0.61.2
numexpr               : None
odfpy                 : None
openpyxl              : None
pandas_gbq            : None
psycopg2              : None
pymysql               : None
pyarrow               : 19.0.1
pyreadstat            : None
pytest                : 8.3.5
python-calamine       : None
pyxlsb                : None
s3fs                  : None
scipy                 : 1.15.2
sqlalchemy            : 2.0.40
tables                : None
tabulate              : 0.9.0
xarray                : None
xlrd                  : None
xlsxwriter            : None
zstandard             : None
tzdata                : 2025.2
qtpy                  : None
pyqt5                 : None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions