Skip to content

BUG: df1.merge(df2, on='ds', how='outer') not convert column ds to the highest resolution datetime type of the two dfs #55212

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

Open
2 of 3 tasks
seanslma opened this issue Sep 20, 2023 · 4 comments
Labels
Bug Non-Nano datetime64/timedelta64 with non-nanosecond resolution Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@seanslma
Copy link

seanslma commented Sep 20, 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

t1 = '2023-09-01'
t2 = '2023-09-01 01:00:00'
t3 = '2023-09-01 01:30:00'
ds1 = pd.date_range(t1, t2, freq='30T')
ds2 = pd.date_range(t1, t3, freq='30T')
df1 = pd.DataFrame({
    'ds': ds1.astype('datetime64[us]'),
    'y1': range(len(ds1)),
})
df2 = pd.DataFrame({
    'ds': ds2.astype('datetime64[ns]'),
    'y2': range(len(ds2)),
})
df3 = df1.merge(df2, on=['ds'], how='outer') #will only convert df2 `ds` type to df1 `ds` type
df3.dtypes

Issue Description

The column type was not converted from the lower resolution (dtype('<M8[us]')) to the higher resolution (dtype('<M8[ns]')).

It only converted the type in df2 to the type in df2 for the on column.

File ~\conda-envs\dev\lib\site-packages\pandas\core\arrays\_mixins.py:398, in NDArrayBackedExtensionArray._putmask(self, mask, value)
    383 """
    384 Analogue to np.putmask(self, mask, value)
    385 
   (...)
    394     If value cannot be cast to self.dtype.
    395 """
    396 value = self._validate_setitem_value(value)
--> 398 np.putmask(self._ndarray, mask, value)

File <__array_function__ internals>:180, in putmask(*args, **kwargs)

TypeError: Cannot cast array data from dtype('<M8[ns]') to dtype('<M8[us]') according to the rule 'safe'

Expected Behavior

I expect df1.merge(df2, on=['ds'], how='outer') and df2.merge(df1, on=['ds'], how='outer') both should work in this case.

Installed Versions

INSTALLED VERSIONS

commit : ba1cccd
python : 3.9.15.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 58 Stepping 0, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_Australia.1252

pandas : 2.1.0
numpy : 1.23.3
pytz : 2022.1
dateutil : 2.8.2
setuptools : 65.5.0
pip : 22.3.1
Cython : 0.29.30
pytest : 7.1.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.0
html5lib : None
pymysql : None
psycopg2 : 2.9.3
jinja2 : 3.0.3
IPython : 8.4.0
pandas_datareader : None
bs4 : 4.11.1
bottleneck : 1.3.5
dataframe-api-compat: None
fastparquet : None
fsspec : 2022.5.0
gcsfs : None
matplotlib : 3.5.2
numba : 0.53.0
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 12.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.1
sqlalchemy : 1.4.46
tables : None
tabulate : None
xarray : 2022.3.0
xlrd : None
zstandard : None
tzdata : 2022.1
qtpy : None
pyqt5 : None

@seanslma seanslma added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 20, 2023
@seanslma seanslma changed the title BUG: BUG: df1.merge(df2, on='ds', how='outer') not convert column ds to the highest resolution datetime type of the two dfs Sep 20, 2023
@paulreece
Copy link
Contributor

I have confirmed locally that this bug occurs on the main development branch.

@rhshadrach rhshadrach added Reshaping Concat, Merge/Join, Stack/Unstack, Explode Non-Nano datetime64/timedelta64 with non-nanosecond resolution labels Sep 22, 2023
@rhshadrach
Copy link
Member

@mroeschke @MarcoGorelli any thoughts on what should happen here?

@mroeschke
Copy link
Member

Yeah as mentioned in the OP this should work by casting to the higher resolution. Concat has as similar problem #53641 (comment)

@rhshadrach rhshadrach removed the Needs Triage Issue that has not been reviewed by a pandas team member label Sep 25, 2023
@jorisvandenbossche jorisvandenbossche added the Regression Functionality that used to work in a prior pandas version label Oct 27, 2023
@jorisvandenbossche jorisvandenbossche added this to the 2.1.3 milestone Oct 27, 2023
@jorisvandenbossche jorisvandenbossche modified the milestones: 2.1.3, 2.1.4 Nov 13, 2023
@lithomas1 lithomas1 removed the Regression Functionality that used to work in a prior pandas version label Dec 3, 2023
@lithomas1
Copy link
Member

I don't think its a regression, I get the same error on 2.0.3, too.

Should have a PR up for 2.1.4, anyways, though.

@lithomas1 lithomas1 modified the milestones: 2.1.4, 2.2 Dec 8, 2023
@lithomas1 lithomas1 modified the milestones: 2.2, 2.2.1 Jan 20, 2024
@lithomas1 lithomas1 modified the milestones: 2.2.1, 2.2.2 Feb 23, 2024
@lithomas1 lithomas1 modified the milestones: 2.2.2, 2.2.3 Apr 11, 2024
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 Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants