0

I want to convert a column in my python dataframe. The column is of type object.

From Dec-21-20 09:20PM

to This 2020-12-21 21:20:00:00.000

Leo Torres
  • 673
  • 1
  • 6
  • 18

2 Answers2

0

df["column"] = pd.to_datetime(df["column"])

If you need more specific formatting, edit your question and comment back so I can adjust my answer accordingly.

zerecees
  • 697
  • 4
  • 13
  • looks great any way I can convert it with out the darn T in the middle? 2020-04-23T19:49:00 – Leo Torres Apr 24 '20 at 03:59
  • I think so, comment back what the result is, and then let me compare to the documentation. I'll stand by. – zerecees Apr 24 '20 at 04:00
  • Interesting the T only appears when I view dataframe what I print it on to screen its not there. – Leo Torres Apr 24 '20 at 04:02
  • Ah ok, that's something with console formatting. Did you need help with console formatting or are you ok with how it prints? – zerecees Apr 24 '20 at 04:03
  • I am fine with how it prints I am new to python. I will be exporting to a sql table will it go with that T as well? I rather not have that T there it will cause issues in SQL if it is expecting a datetime. The T will create type error. Thank you for your help! – Leo Torres Apr 24 '20 at 04:09
  • It shouldn't, but it will all depend on your parameters. Try [df.to_sql](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.html) and see what prints in the console. – zerecees Apr 24 '20 at 04:12
0

If you are looking to convert it into dtype: datetime64[ns]

then use: df["column"] = pd.to_datetime(df["column"])

or specify your format

pd.to_datetime(pd.Series(['05/23/2005']), format="%m/%d/%Y")
Out[12]:
0   2005-05-23
dtype: datetime64[ns]

once you have that you can use pandas.Series.dt.strftime to any format of string you want

Kuldeep Singh Sidhu
  • 3,748
  • 2
  • 12
  • 22