Specify datetime dtype when Reading CSV as pandas DataFrame in Python (Example)

 

In this article, you’ll learn how to set a datetime dtype while importing a CSV file to a pandas DataFrame in Python programming.

To be more specific, the content of the article looks as follows:

Let’s dive right in:

 

Example Data & Add-Ons

First, we need the datetime module and the pandas library, so let’s import them.

import pandas as pd                                                # Loading pandas library
import datetime                                                    # Loading datetime module

Let’s also construct some example data:

df = pd.DataFrame({"column_1" : [datetime.date(2018, 8, 10),       # Sample data initialization
                            datetime.date(2020, 10, 12),
                            datetime.date(2015, 2, 14),
                            datetime.date(2012, 3, 19),
                            datetime.date(2018, 5, 23)],
                    "column_2" : [datetime.datetime(2018, 8, 10, 10, 7, 10),
                            datetime.datetime(2020, 10, 12, 16, 9, 30),
                            datetime.datetime(2015, 2, 14, 21, 14, 43),
                            datetime.datetime(2012, 3, 19, 23, 12, 55),
                            datetime.datetime(2018, 5, 23, 15, 9, 8)]
                            })
df.to_csv('sample_csv.csv', index=False)                           # Converting the constructed DataFrame to CSV

For the sake of creating an example CSV, our data was created as a DataFrame first, and then it was converted to a CSV file.

 

Example: Setting dtype of date/datetime Objects while Importing CSV to pandas DataFrame

Have a look at the Python code and its output below. It shows that the dates in the example data are parsed as strings if the data type is not specified explicitly.

df_default = pd.read_csv("sample_csv.csv")                         # importing the CSV directly
print(df_default.dtypes)                                           # Printing the dtypes of DataFrame columns
# column_1    object
# column_2    object
# dtype: object
print(type(df_default["column_1"][0]))                             # Printing the type of element in column_1
# <class 'str'>

The following Python code shows how to set parsing instructions for the selected columns of the CSV file via the argument parse_dates.

parse_dates = ["column_1", "column_2"]                             # setting column names to be parsed
df_parse = pd.read_csv("sample_csv.csv", parse_dates=parse_dates)  # reading the CSV
print(df_parse.dtypes)                                             # printing the dtypes of each column of DataFrame
# column_1    datetime64[ns]
# column_2    datetime64[ns]
# dtype: object

As seen above, now we have column_1 and column_2 parsed as datetime objects instead of strings.

 

Video & Further Resources

Do you need further explanations on the contents of this tutorial? Then I recommend watching the following video on my YouTube channel. I explain the content of this article in the video.

 

The YouTube video will be added soon.

 

Furthermore, you may read the related tutorials on my homepage:

 

Summary: In this article, you have learned how to set datetime dtypes while reading a CSV as a pandas DataFrame in the Python programming language. Please let me know in the comments section, in case you have additional questions.

 

Ömer Ekiz Python Programming & Informatics

This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get further information about his professional background, a list of all his tutorials, as well as an overview on his other tasks on Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top