0

I'm quite new to Python. I have a dataframe with two columns. The second column is from type Series. They all have a suffix I want to delete. I tried to convert it to a string and then maximize the string size. But nothing worked.

I tried: .

df['id'].astype(basestring)

df['id']= df['id'].astype(str)

df['id'] = df['id'].astype("string")

df['id'] = pandas.Series(df['id'], dtype="string")

df['id'] = pandas.Series(df['id'], dtype=pandas.StringDtype)

The column looks like this:

28.04.2019 10:00

29.04.2019 13:00

30.04.2019 14:00
...

I just want the dates and delete the time. Is there any other option I could use to reach my aim?

Thank you in advance!

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Lisa
  • 43
  • 4
  • Check this thread. https://stackoverflow.com/questions/16176996/keep-only-date-part-when-using-pandas-to-datetime – Tony Cheung Jun 09 '21 at 16:05

1 Answers1

1

1.If the type of ID column is datetime, then try

df['id'] = df['id'].dt.date

2.If the type is string, then first convert it into datetime and then remove the timestamp

df['id'] = pd.to_datetime(df['id'], format = '%d.%m.%Y %H:%M').dt.date
DumbCoder
  • 233
  • 2
  • 9
  • Thank you for your help. Unfortunately both don't work. The error for the first try is: AttributeError: Can only use .dt accessor with datetimelike values And the second: KeyError: "None of [Int64Index([67], dtype='int64')] are in the [columns]" Is there anything else I can ry? – Lisa Jun 10 '21 at 06:48
  • Can you tell me exactly in which format data is present in column 'ID'? – DumbCoder Jun 10 '21 at 10:57