Home > OS >  convert a Dataframe of date objects to datetime
convert a Dataframe of date objects to datetime

Time:10-02

print(data["Date Time"]

enter image description here

data["Date Time"] = data["Date Time"].str.replace("Verkauft","")

print(data["Date Time"]

enter image description here

above DataFrame is dtype: object

i want to convert into datetime to use it for visualization

data["Date Time"] = pd.to_datetime(data["Date Time"],format="%d%M%Y")

Error Output:

enter image description here

Update:

i added:

data["Date Time"] = data["Date Time"].str.replace("  ","")

because the output shows blank characters. however, it did not help.

Output:

enter image description here

CodePudding user response:

Try to remove format= to let the function determine the right format:

data = pd.DataFrame({'Date Time': ['1. Sep 2021']})
data['Date Time'] = pd.to_datetime(data["Date Time"])
print(data)

# Output:
   Date Time
0 2021-09-01

Update:

I got error again: "Unknown string format: %s, timestt"

To debug, find rows where dates have not a right format:

data.loc[pd.to_datetime(data['Date Time'], errors='coerce').isna(), 'Date Time']
  • Related