0

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
  • A csv file is pure text. When you read it in, pandas does its best to figure out what type each column should be. So if you read it in, change a type, and print it back out, you may not have really done anything. Luckily, the `.read_csv()` method has a ton of options for reading things in the way you want: https://stackoverflow.com/questions/17465045/can-pandas-automatically-read-dates-from-a-csv-file – Vincent Rupp Nov 01 '22 at 19:36
  • please provide an example of the data in the dataframe. – D.L Nov 01 '22 at 19:37
  • 1
    Your question needs a minimal reproducible example consisting of sample input, expected output, actual output, and only the relevant code necessary to reproduce the problem. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for best practices related to Pandas questions. – itprorh66 Nov 01 '22 at 20:00
  • Edited. Hope that helps... ? – Guillermo Kelly Nov 01 '22 at 23:21

0 Answers0