Hope this isn't a repost or something like that. Been meddling with pandas for some time, am trying to change several column dtypes in order to make it more manageable. One of this changes is the dtype of a given column, when using:
df = pd.DataFrame(data)
print (df.dtypes)
The console shows the dtype is an "object". So far so good, was to change it anyways. Then I go and use this to convert the column to datetime:
df[["Open Date", "Close Date"]] = df[["Open Date", "Close Date"]].apply(pd.to_datetime)
print (df.dtypes)
This makes the console show the dtype "changed" to datetime64[ns] (not sure yet if I can work with that yet anyways).
The problem is AFTER this line, when trying to save it to a CSV file because saves nothing!
df.to_csv ("3thParty.csv", index = None, header=True)
Running the produced CSV file with the first 2 lines of code shown before returns the "object" dtype. So, What is this one doing wrong?
Already tried several refactors to no avail, sadly, no other file extensions could be used to save the df as the csv is indispensable, so it seems either my cfg is wrong or the df is not saving in the file.
This is the full code:
import pandas as pd
# Read CSV file
data = pd.read_csv("3thParty.csv")
# Convert it to a DataFrame object & print type
df = pd.DataFrame(data)
print (df.dtypes)
# Convert the DataFrame date object to datetime[ns]
df[["Open Date", "Close Date"]] = df[["Open Date", "Close Date"]].apply(pd.to_datetime)
print (df.dtypes)
# Write the dataframe object into a csv file
df.to_csv ("3thParty.csv", index = None, header=True)
Cannot post images as I'm under 10 reputation... U.U
EDIT: The columns I try to change from "object" to "datetime" contained in the dataset:
Open Date Close Date
04/06/2021 13/07/2021
04/06/2021 13/07/2021
24/06/2021 18/08/2021
02/08/2021 08/09/2021
Being the output equal as above, the only thing that changes is the output in the console:
**Input:**
Service Request Number int64
Severity object
TGCS SR Summary object
Employee Name object
Days Open int64
Open Date object
Close Date object
Product Group object
Product object
Resolution object
dtype: object
**Output:**
Service Request Number int64
Severity object
TGCS SR Summary object
Employee Name object
Days Open int64
Open Date **datetime64[ns]**
Close Date **datetime64[ns]**
Product Group object
Product object
Resolution object
dtype: object