Skip to content

BUG: pandas cannot recognize date objects as datetime like values #46582

Open
@dcsaba89

Description

@dcsaba89

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 datetime as dt
import pandas as pd

d1 = dt.datetime(2020, 10, 10)
year1 = d1.year  # This works, year1 == 2020

d2 = dt.date(2020, 10, 10)
year2 = d2.year  # This works, year2 == 2020

df = pd.DataFrame({'DATE_STR': ['2020-10-10']})
df['DATETIME'] = pd.to_datetime(df['DATE_STR'])
df['DATE'] = df['DATETIME'].dt.date


print(df.to_string(index=False))

yearA = df['DATETIME'].dt.year # This works
yearB = df['DATE'].dt.year  # AttributeError: Can only use .dt accessor with datetimelike values. Did you mean: 'at'?

Issue Description

Pandas cannot recognize datetime.date(...) objects as datetime like values.

pandas.Series.dt.year, pandas.Series.dt.month, pandas.Series.dt.day, etc ... does not work with dt.date(...) objects.

Expected Behavior

df['DATETIME'].dt.year and df['DATE'].dt.year must be identical if df['DATETIME'] and df['DATE'] contains the same dates as datetime and date objects respectively.

In general, all pd.Series.dt methods like day, month, year, etc that replicates datetime methods for pd.Series should return the same output as the underlying datetime methd.

For example if dt.datetime(2020, 10, 10).month and dt.date(2020, 10, 10).month both return 10, then pd.Series([dt.datetime(2020, 10, 10)]).dt.month and pd.Series([dt.date(2020, 10, 10)]).dt.month should be identical and should return the same value ( = 10) as the underlying datetime methods.

Installed Versions

INSTALLED VERSIONS

commit : 945c9ed
python : 3.9.7.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22000
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 1.3.4
numpy : 1.20.3
pytz : 2021.3
dateutil : 2.8.2
pip : 21.2.4
setuptools : 58.0.4
Cython : 0.29.24
pytest : 6.2.4
hypothesis : None
sphinx : 4.2.0
blosc : None
feather : None
xlsxwriter : 3.0.1
lxml.etree : 4.6.3
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.29.0
pandas_datareader: None
bs4 : 4.10.0
bottleneck : 1.3.2
fsspec : 2021.10.1
fastparquet : None
gcsfs : None
matplotlib : 3.4.3
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.7.1
sqlalchemy : 1.4.22
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : 1.3.0
numba : 0.54.1

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions