2

I want to change the format of "Date" column from 10/15/2019 to m/d/y format.

tax['AsOfdate']= pd.to_datetime(tax['date'])

How do I do it?

  • Possible duplicate of [How to define format when use pandas to\_datetime?](https://stackoverflow.com/questions/36848514/how-to-define-format-when-use-pandas-to-datetime) – gosuto Oct 25 '19 at 21:27

3 Answers3

1

like this, and here is the documentation.

tax['AsOfdate']= pd.to_datetime(tax['date'], format="%m/%d/%Y" )
Florian Bernard
  • 2,561
  • 1
  • 9
  • 22
0

Here is an example with today's date formatting:

from datetime import date
today = date.today()
new_format = today.strftime("%m/%d/%y")
print(today, new_format)
Polto
  • 95
  • 1
  • 10
0

Pandas to_datetime function accepts a format command which accepts strftime notation.

Pandas docs: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html

Strftime docs: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

m/d/y notation would be:

tax['AsOfdate']= pd.to_datetime(tax['date'], format='%m/%d/%y)

Assuming you want everything zero padded with two digits like 01/01/19 for January first 2019. If you need something else, the strftime formatting link shows all the codes that let you choose padding or not, four-digit year or two-digit, and so on.

Ryan
  • 1,032
  • 1
  • 10
  • 23