Skip to content

BUG: Inconsistent datetime dtype precision with assign() using list vs direct value #56733

Closed
@glichtner

Description

@glichtner

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
import datetime

dt = datetime.datetime(2024, 1, 4, 12, 0, 0)

df = pd.DataFrame(index=[0])

df_list = df.assign(datetime=[dt])
df_value = df.assign(datetime=dt)

pd.testing.assert_frame_equal(df_list, df_value)

Issue Description

The dtype of the datetime column should be the same in both cases, but it is different:

AssertionError: Attributes of DataFrame.iloc[:, 0] (column name="datetime") are different

Attribute "dtype" are different
[left]:  datetime64[ns]
[right]: datetime64[us]

Expected Behavior

No assertion error.

Installed Versions

INSTALLED VERSIONS

commit : 2522b0a
python : 3.11.5.final.0
python-bits : 64
OS : Linux
OS-release : 6.5.0-14-generic
Version : #14-Ubuntu SMP PREEMPT_DYNAMIC Tue Nov 14 14:59:49 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.3.0.dev0+43.g2522b0ab1b
numpy : 2.0.0.dev0+git20240102.d906b52
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.2.2
pip : 23.3.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.15.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
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 : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.4
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNon-Nanodatetime64/timedelta64 with non-nanosecond resolution

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions