1

I have a dataset in CSV which first column are dates (not datetimes, just dates).

The CSV is like this:

date,text
2005-01-01,"FOO-BAR-1"
2005-01-02,"FOO-BAR-2"

If I do this:

df = pd.read_csv('mycsv.csv')

I get:

print(df.dtypes)

date       object
text       object
dtype: object

How can I get column date by datetime.date?

M.E.
  • 4,955
  • 4
  • 49
  • 128
  • Those this help answering your question https://stackoverflow.com/questions/21269399/datetime-dtypes-in-pandas-read-csv – Simon Hawe Jan 16 '22 at 19:04

2 Answers2

1

You can use pd.to_datetime function available in pandas.

For example in a dataset about scores of a cricket match. I can convert the Matchdate column to datatime object by applying pd.to_datetime function based on the data time format given in the data. ( Refer https://www.w3schools.com/python/python_datetime.asp to assign commands based on your data time formating )

 cricket["MatchDate"]=pd.to_datetime(cricket["MatchDate"], format= "%m-%d-%Y") 
Charles
  • 37
  • 7
1

Use:

df = pd.read_csv('mycsv.csv', parse_dates=[0])

This way the initial column will be of native pandasonic datetime type, which is used in Pandas much more often than pythonic datetime.date.

It is a more natural approach than conversion of the column in question after you read the DataFrame.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
  • Thanks, this is really useful. When you say native pandasonic datetime type, is it the same as numpy datetime64? I have followed your advise and it seems that Pandas reports such type for the date column. – M.E. Jan 29 '22 at 23:59
  • 1
    *datetime64* in *Pandas* is roughly the same as in *Numpy*. An important difference is that in *Numpy* you can set various resolutions (e.g. day or millisecond), whereas in *Pandas* the only resolution supported is nanosecond (*ns*). Under the hood, *Pandas* keeps this type as a signed number of nanoseconds since the start of the Unix epoch. This is why *pandasonic* datetime range is roughly 22.09.1677 – 11.04.2262. If you attempt to save in *Pandas* dates outside this range, an exception occurs. – Valdi_Bo Jan 30 '22 at 07:02