-1

I am trying to do create a column that has a year, month, weekdays in the dateformat

enter image description here

like 2020-10-Thu or 2020-10-04 but tha both data will be in date format ther is any way to do that thing

Noman
  • 135
  • 1
  • 6
  • what is the issue you are running in to – gold_cy Jan 12 '22 at 12:57
  • why do you post a screenshot instead of text? what is "dateformat", do you mean datetime data type? what did you try? – FObersteiner Jan 12 '22 at 12:58
  • I am trying to create a DateTime format with week but if use strftime and something else it converts this date-time into a string 2020-10-Thu i need it should be in date format there is anyway to do this @MrFuppes – Noman Jan 21 '22 at 07:49
  • Ok could you please clarify, what does WOD mean? – FObersteiner Jan 21 '22 at 08:19

1 Answers1

1

If I understood your question correctly, you want to have a column that contains the date in the format of 2020-10-Thu or 2020-10-04? If that's the case, the code below should work for you (Just note that the First Format and Second Format columns end up being of type string, also note that the weekdays begin with 1 where sunday is labelled as 0).

from datetime import datetime
import pandas as pd

originalDates = pd.date_range(start="2020-02-02",end="2020-03-03")
firstFormat = pd.Series([datetime.strftime(x, '%Y-%m-%a') for x in originalDates])
secondFormat = pd.Series([datetime.strftime(x, '%Y-%m-%w') for x in originalDates])

frame = {'Original Dates': originalDates, 'First Format': firstFormat, 'Second Format': secondFormat}

df = pd.DataFrame(frame)

The df variable ends up looking like this:

Original Dates First Format Second Format
2020-02-03 2020-02-Mon 2020-02-1
2020-02-04 2020-02-Tue 2020-02-2
2020-02-05 2020-02-Wed 2020-02-3

I'm sure there's a better way to do this, but I'm not aware of it at this time.

  • 1
    hey thanks for your solution and your effort I appreciate it but that data is in string format i need that data in date format there is any way – Noman Jan 21 '22 at 07:46
  • @Noman Sorry, unfortunately I don't know how to do that. I have tried converting the resulting strings to datetime objects with the strptime() function, but that reverts the day part of the date back into the day of the month format. – Jovan Topolac Jan 22 '22 at 18:06