Skip to content
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

BUG: Concatenating datetime with different resolution fails #55067

Closed
2 of 3 tasks
Tracked by #5
robert-schmidtke opened this issue Sep 8, 2023 · 5 comments
Closed
2 of 3 tasks
Tracked by #5

BUG: Concatenating datetime with different resolution fails #55067

robert-schmidtke opened this issue Sep 8, 2023 · 5 comments
Labels
Bug Non-Nano datetime64/timedelta64 with non-nanosecond resolution Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@robert-schmidtke
Copy link
Contributor

robert-schmidtke commented Sep 8, 2023

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
df = pd.DataFrame({"col": [pd.Timestamp.now()]}, dtype="datetime64[ns]")
df2 = pd.DataFrame({"col": [pd.Timestamp.now(), pd.Timestamp.now()]}, dtype="datetime64[us]")
pd.concat([df, df2])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/rschmidtke/mambaforge/envs/myenv/lib/python3.11/site-packages/pandas/core/reshape/concat.py", line 393, in concat
    return op.get_result()
           ^^^^^^^^^^^^^^^
  File "/home/rschmidtke/mambaforge/envs/myenv/lib/python3.11/site-packages/pandas/core/reshape/concat.py", line 680, in get_result
    new_data = concatenate_managers(
               ^^^^^^^^^^^^^^^^^^^^^
  File "/home/rschmidtke/mambaforge/envs/myenv/lib/python3.11/site-packages/pandas/core/internals/concat.py", line 189, in concatenate_managers
    values = _concatenate_join_units(join_units, copy=copy)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rschmidtke/mambaforge/envs/myenv/lib/python3.11/site-packages/pandas/core/internals/concat.py", line 486, in _concatenate_join_units
    concat_values = concat_compat(to_concat, axis=1)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rschmidtke/mambaforge/envs/myenv/lib/python3.11/site-packages/pandas/core/dtypes/concat.py", line 132, in concat_compat
    return cls._concat_same_type(to_concat_eas)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rschmidtke/mambaforge/envs/myenv/lib/python3.11/site-packages/pandas/core/arrays/datetimelike.py", line 2246, in _concat_same_type
    new_obj = super()._concat_same_type(to_concat, axis)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rschmidtke/mambaforge/envs/myenv/lib/python3.11/site-packages/pandas/core/arrays/_mixins.py", line 230, in _concat_same_type
    return super()._concat_same_type(to_concat, axis=axis)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "arrays.pyx", line 190, in pandas._libs.arrays.NDArrayBacked._concat_same_type
ValueError: all the input array dimensions except for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 1 and the array at index 1 has size 2

Issue Description

When concatenating datetime columns with different resolution, e.g. ns and us, an array dimension error is raised.

Expected Behavior

Up until pandas 2.0, this would result in the object dtype on the result

>>> import pandas as pd
>>> df = pd.DataFrame({"col": [pd.Timestamp.now()]}, dtype="datetime64[ns]")
>>> df2 = pd.DataFrame({"col": [pd.Timestamp.now(), pd.Timestamp.now()]}, dtype="datetime64[us]")
>>> pd.concat([df, df2])
                          col
0  2023-09-08 16:49:44.876710
0  2023-09-08 16:49:49.084212
1  2023-09-08 16:49:49.084230
>>> pd.concat([df, df2]).dtypes
col    object
dtype: object

Installed Versions

INSTALLED VERSIONS

commit : ba1cccd
python : 3.11.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.90.1-microsoft-standard-WSL2
Version : #1 SMP Fri Jan 27 02:56:13 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.1.0
numpy : 1.25.2
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.1.2
pip : 23.2.1
Cython : None
pytest : 7.4.2
hypothesis : None
sphinx : 7.2.5
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.15.0
pandas_datareader : None
bs4 : 4.12.2
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 13.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@robert-schmidtke robert-schmidtke added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 8, 2023
@rhshadrach rhshadrach added the Regression Functionality that used to work in a prior pandas version label Sep 10, 2023
@rhshadrach rhshadrach added this to the 2.1.1 milestone Sep 10, 2023
@paulreece
Copy link
Contributor

I have tested this on 2.1.0 and can confirm it is a bug.

@ccfreeman
Copy link

I am seeing a similar issue, but it appears to be more related to Pyarrow 13.0.0. Pandas==2.1.0 and Pyarrow==12.0.0 works fine, while Pandas==2.1.0 and Pyarrow==13.0.0 throws this error.

@jorisvandenbossche jorisvandenbossche added Non-Nano datetime64/timedelta64 with non-nanosecond resolution and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 15, 2023
@hkhojasteh
Copy link
Contributor

hkhojasteh commented Sep 21, 2023

@jorisvandenbossche I with investigate the issue and in case it will be related to pandas itself, I will try to fix it :)

@ericwbzhang
Copy link

what is the status of this issue? I got hit in 2.1.4 and it seems good in 2.2.0rc0 but it is not mentioned in whatsnew https://pandas.pydata.org/pandas-docs/version/2.2.0rc0/whatsnew/v2.2.0.html

@lithomas1
Copy link
Member

This is probably fixed by #53641.

(That PR is missing a whatsnew entry, though. I'll put up a PR adding the missing whatsnew entry)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Non-Nano datetime64/timedelta64 with non-nanosecond resolution Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

No branches or pull requests

8 participants