1

I have a df with a column 'start date'. It has date values. They are of string type. The column also has blank values.

--> df.loc[0,'start date']
output : '2022-10-04'
--> type(df.loc[0,'start date'])
output : str

I have imported datetime

import datetime

And I try to convert this entire column to pandas datetime type.

df['start date'] = pd.to_datetime(df['start date'], errors='coerce')

Because this will give time stamp as well, I want to keep only the date, I did this:

df['start date'] = pd.to_datetime(df['start date'], errors='coerce').datetime.date

But this gives an Attribute error :AttributeError: 'Series' object has no attribute 'datetime' Whereas if I tweak the code to :

df['start date'] = pd.to_datetime(df['start date'], errors='coerce').dt.date

It works!

What's happening?! Any help in understanding the concept behind this would be appreciated :)

Dima Chubarov
  • 16,199
  • 6
  • 40
  • 76
Soumya Pandey
  • 321
  • 3
  • 19
  • 5
    because the accessor is named `dt` and not `datetime`… this is a design choice of pandas developers, nothing more – mozway Nov 21 '22 at 14:06
  • @mozway when I write import datetime, I am importing a module which has functions, classes, and variables. Right? – Soumya Pandey Nov 21 '22 at 14:09
  • 1
    @SoumyaPandey you don't need to import datetime yourself, the pandas dt attribute works on its own through the pandas package – maxxel_ Nov 21 '22 at 14:11
  • https://stackoverflow.com/a/68672660/1328439 related question on how to keep only the date part of a pandas Series. – Dima Chubarov Nov 21 '22 at 14:12
  • `import datetime` imports the Python standard library module 'datetime' - `pandas` doesn't use that to represent date/time, see [the docs](https://pandas.pydata.org/docs/user_guide/timeseries.html). However, you can use it *in combination* with pandas datetime. – FObersteiner Nov 21 '22 at 18:55

0 Answers0