1

I have Python Pandas DataFrame like below:

col1
----
01.05.2019
02.11.2022
11.08.2001

And col1 is in "object" format.

I need to read this Data Frame from csv and in "read_csv" function modify:

  1. format of this column from "object" to "date"
  2. form of presenting values, because when I use only: df= pd.read_csv("df.csv", delimiter=";", index_col=0, parse_dates=['col1']) format of "col1" is "date" but the form is changed to for example: "2019-05-01"

My code is as below:

df= pd.read_csv("df.csv", delimiter=";", index_col=0, parse_dates=['col1'], date_parser=lambda x: pd.to_datetime(x, format='%d.%m.%Y'))

How can I modify my code based of my needs ?

  1. Currently in cas I have form of
  2. date like: 01.05.2019
  3. Nevertheless, it is "object"
  4. I would like to modify format od column from "object" to "date" and still have values in column "date" in format like: 01.05.2019
  5. Neveerthleess, when I use my code I finally have format of data like "datetime" but form is not good because like: 2019-05-01.
dingaro
  • 2,156
  • 9
  • 29
  • When you read the csv using Pandas the col date is formatted as a date. What exactly is the issue? From your description it is unclear what you are trying to do. Maybe give us an example of what format you want the for the date? – itprorh66 May 30 '22 at 21:09
  • itprorh66 I modified my question and I added 4 point which descripes my problem better :) – dingaro May 30 '22 at 21:12
  • Why does the *visual* output of the `datetime` column matter? `2019-05-01` is a correct format and is how `datetimes` are displayed. If you need a different output for export after performing calculations, ako's method below demonstrates how to do that. – BeRT2me May 30 '22 at 21:50

1 Answers1

1

If this is just a question of formatting the date and not parsing it - you can do as follows:

import io

some_data = """a   01.05.2019
b   02.11.2022
c   11.08.2001"""

# read data, ensuring parsing as datetime64
some_df = pd.read_csv(io.StringIO(some_data), delimiter='\s+', names=['ix', 'date'], parse_dates=[
                      'date'], date_parser=lambda x: pd.to_datetime(x, format='%d.%m.%Y'))


# format the date to a string as desired
some_df['date_formated'] = some_df.date.apply(lambda x: x.strftime('%d.%m.%Y'))
some_df

  ix       date date_formated
0  a 2019-05-01    01.05.2019
1  b 2022-11-02    02.11.2022
2  c 2001-08-11    11.08.2001
ako
  • 3,569
  • 4
  • 27
  • 38