How do i turn a object into a datetime in python?

Below is the first row of my csv DateTime column:

Mon Nov 02 20:37:10 GMT+00:00 2015

The DateTime column is currently an object and I want to convert it to datetime format so that I can get the date to appear as 2015-11-02 and I will create a separate column for the time.

The code I am using to convert the column to date time format is:

for item, frame in df['DateTime'].iteritems():
     datetime.datetime.strptime(df['DateTime'], "%a-%b-%d-%H-%M-%S-%Z-%Y")

I am getting this error:

> TypeError: must be str, not Series

Any help would be greatly appreciated!

asked Jul 12, 2016 at 16:04

2

Use pd.to_datetime():

df['DateTime'] = pd.to_datetime(df['DateTime'])

For example,

pd.to_datetime('Mon Nov 02 20:37:10 GMT+00:00 2015')

produces Timestamp('2015-11-02 20:37:10').

answered Jul 12, 2016 at 16:08

How do i turn a object into a datetime in python?

1

Another solution is to pass errors parameter as per below and its application

df['DateTime'] = pd.to_datetime(df['DateTime'], errors='coerce')

errors{‘ignore’, ‘raise’, ‘coerce’}, default ‘raise’

  1. If ‘raise’, then invalid parsing will raise an exception.
  2. If ‘coerce’, then invalid parsing will be set as NaN.
  3. If ‘ignore’, then invalid parsing will return the input.

answered Sep 8 at 9:26

How do i turn a object into a datetime in python?

BabulalBabulal

3213 silver badges11 bronze badges


In this article, we will discuss how to convert a date to a datetime object in python.

We use the combine method from the datetime module to combine a date and time object to create a datetime object.

Syntax

The syntax of combine() method is as follows.

datetime.combine(date,time)

Where,

  • date is a date object.
  • time is a time object.

This method returns a datetime object which is the combination of the above two objects(date,time).

Converting a date object to datetime object

If we have a date object and don't have a time object, then you initialize the time object to a minimum using the datetime object (Minimum means at midnight i.e 00:00:00).

Example

In the below example code, we combine a date object is retrieved by using the today() method and we initialized the time object to minimum time(00:00:00) using the min.time() method. And combined these two objects by applying the datetime.combine() method.

from datetime import date from datetime import datetime my_date = date.today() print("The date object:",my_date) my_time = datetime.min.time() print("The time object:",my_time) my_datetime = datetime.combine(my_date, my_time) print("The combined datetime object:",my_datetime)

Output

The output of the above code is as follows.

The date object: 2022-05-19
The time object: 00:00:00
The combined datetime object: 2022-05-19 00:00:00

When both date and time objects are given

Here, we initialize both date and time objects. And combine these into a datetime object using the combine method.

Example

In this example, we will convert a datetime object when both date and time objects are given.

import datetime my_date = datetime.datetime(2012, 2, 12) print("The date object:",my_date) my_time = datetime.time(1, 30) print("The time object:",my_time) combined_datetime = my_date.combine(my_date, my_time) print("The combined datetime object:",combined_datetime)

Output

The output of the above code is as follows;

The date object: 2012-02-12 00:00:00
The time object: 01:30:00
The combined datetime object: 2012-02-12 01:30:00

Using the datetime()

The datetime() constructor in python accepts year, month and date values as parameters and create a datetime object.

Pass the year, month and day values of the desired date object (as my_date.year, my_date.month, my_date.day) to this constructor to convert a date object to datetime object.

Example

from datetime import date from datetime import datetime my_date = date.today() my_datetime = datetime(my_date.year, my_date.month, my_date.day) print(my_datetime)

Output

2022-09-05 00:00:00

How do i turn a object into a datetime in python?

Updated on 08-Sep-2022 06:58:03

  • Related Questions & Answers
  • How to convert Python date string mm/dd/yyyy to datetime?
  • How to convert JS date time to MySQL datetime?
  • MySQL Query to convert from datetime to date?
  • How to convert Python datetime to epoch with strftime?
  • How to convert timestamp string to datetime object in Python?
  • How to convert Python DateTime string into integer milliseconds?
  • How to convert timestamp to datetime in MySQL?
  • How to convert TimeStamp to DateTime in Kotlin?
  • How to convert pandas offset to Python date?
  • How to convert Python date to Unix timestamp?
  • How to convert a Python date string to a date object?
  • How to convert a JS Date to a Python date object?
  • How to convert Python date in JSON format?
  • How to convert MySQL datetime to Unix timestamp?
  • Convert string to DateTime and vice-versa in Python

How do you create a date object in Python?

To create a date, we can use the datetime() class (constructor) of the datetime module. The datetime() class requires three parameters to create a date: year, month, day.

How do you convert to time in Python?

We can convert a string to datetime using strptime() function. This function is available in datetime and time modules to parse a string to datetime and time objects respectively.

How do I convert text to date in Python?

from datetime import datetime..
date_time_str = '18/09/19 01:55:19'.
date_time_obj = datetime. strptime(date_time_str, '%d/%m/%y %H:%M:%S').
print ("The type of the date is now", type(date_time_obj)).

Is datetime an object in Python?

Date and datetime are an object in Python, so when you manipulate them, you are actually manipulating objects and not string or timestamps.