Python Forum
Formatting date in a dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Formatting date in a dataframe
#1
Hi guys,

I have a pandas generated dataframe (df) where the first column has a series of dates as below:

Output:
0 11.26.2020 03:36 1 12.01.2020 15:00 2 12.01.2020 21:30 3 12.02.2020 13:26 4 12.04.2020 22:43 5 12.21.2020 00:05
I need to change these dates to a format YY,MM,DD H:M
If I look at the raw values in the df array:
df.values[:,0]
Output:
array(['11.26.2020 03:36', '12.01.2020 15:00', '12.01.2020 21:30', '12.02.2020 13:26', '12.04.2020 22:43', '12.21.2020 00:05'], dtype=object)
We need to change these values to a DateTimeIndexArray and then parse to the correct string format:

dti = pd.to_datetime(df.values[:,0])
dti = dti.strftime('%Y.%m.%d %H:%M')
dti.values


This code gives this output:

Output:
array(['2020.11.26 03:36', '2020.12.01 15:00', '2020.12.01 21:30', '2020.12.02 13:26', '2020.12.04 22:43', '2020.12.21 00:05'], dtype=object)
So it looks like it's done what I want!
Copying back the re-formatted dates to the df:

df.values[:,0] = dti.values
This does not work! The dates remain in the original format.

Any ideas please?
Reply
#2
(Jan-05-2021, 05:56 PM)WiPi Wrote: Hi guys,

I have a pandas generated dataframe (df) where the first column has a series of dates as below:

Output:
0 11.26.2020 03:36 1 12.01.2020 15:00 2 12.01.2020 21:30 3 12.02.2020 13:26 4 12.04.2020 22:43 5 12.21.2020 00:05
I need to change these dates to a format YY,MM,DD H:M
If I look at the raw values in the df array:
df.values[:,0]
Output:
array(['11.26.2020 03:36', '12.01.2020 15:00', '12.01.2020 21:30', '12.02.2020 13:26', '12.04.2020 22:43', '12.21.2020 00:05'], dtype=object)
We need to change these values to a DateTimeIndexArray and then parse to the correct string format:

dti = pd.to_datetime(df.values[:,0])
dti = dti.strftime('%Y.%m.%d %H:%M')
dti.values


This code gives this output:

Output:
array(['2020.11.26 03:36', '2020.12.01 15:00', '2020.12.01 21:30', '2020.12.02 13:26', '2020.12.04 22:43', '2020.12.21 00:05'], dtype=object)
So it looks like it's done what I want!
Copying back the re-formatted dates to the df:

df.values[:,0] = dti.values
This does not work! The dates remain in the original format.

Any ideas please?

Just for completion and if it's any use to anyone I did figure this out. I just directly updated the column rather than using a dummy variable (dti). Column name is 'Open Date' and the code is below which works.
No idea why the original code stopped working as it had been fine up until now! The main issue was reassigning the new values back to the original.
df['Open Date']=pd.to_datetime(df.values[:,0])                  #convert the 'date' column to DateTimeIndex array
df['Open Date']= df['Open Date'].dt.strftime('%Y.%m.%d %H:%M')  #parse to string and set date format
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 220 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 589 Jan-20-2024, 04:45 AM
Last Post: 1418
  Formatting a date time string read from a csv file DosAtPython 5 1,254 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  Need help formatting dataframe data before saving to CSV cubangt 16 5,787 Jul-01-2022, 12:54 PM
Last Post: cubangt
  Filter dataframe by datetime.date column glidecode 2 5,108 Dec-05-2021, 12:51 AM
Last Post: glidecode
  Date format and past date check function Turtle 5 4,235 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,111 May-04-2021, 10:51 PM
Last Post: rhat398
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 2,231 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  Confusion about [date]time [formatting] Mark17 0 1,737 Dec-17-2020, 07:25 PM
Last Post: Mark17
  How to add date and years(integer) to get a date NG0824 4 2,858 Sep-03-2020, 02:25 PM
Last Post: NG0824

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020