1

I believe that my problem is really straightfoward and there must be a really easy way to solve this issue with pandas that I am still not aware of.

The problem is that I have one column on a pandas dataframe which all the elements are written on this following format:

 yyyy-MM-ddT00:00:00

I want to translate each element of the column to the following format:

yyyy-MM-dd 00:00:00

And also, afterwards, is it possible from this column of the dataframe, create two more columns now dividing into date and time. What I mean is, to get:

yyyy-MM-dd 00:00:00

to two more columns, one containing the date:

yyyy-MM-dd

And the other containing the time:

00:00:00

Hope that I could synthetize everything properly. Thank you in advance.

Marc Schwambach
  • 438
  • 4
  • 20
  • Possible duplicate of [How do I split a string into several columns in a dataframe with pandas Python?](https://stackoverflow.com/questions/48958282/how-do-i-split-a-string-into-several-columns-in-a-dataframe-with-pandas-python) – SpghttCd Aug 29 '19 at 09:31

1 Answers1

1

How about pd.Series.str.split

# import pandas as pd
# df = pd.DataFrame({'Date': ['2019-08-29T12:00:00']})

#                   Date
# 0  2019-08-29T12:00:00

df.Date.str.split('T', expand=True)

#             0         1
# 0  2019-08-29  12:00:00

To simply get rid of the T between date and time string, you could use pd.Series.str.replace:

df.Date.str.replace('T', ' ')

# 0    2019-08-29 12:00:00
SpghttCd
  • 10,510
  • 2
  • 20
  • 25
  • Thank you SpghttCd. Do you also know how would it work if I wanted to put the data on this format - yyyy-MM-dd 00:00:00 - on the same column? – Marc Schwambach Aug 29 '19 at 10:21