2

I currently have a dataframe table shown below:

Day_Part    Start_Time    End_Time
Quarter 1   2014, 1, 1    2015, 3, 1
Quarter 2   2014, 3, 3    2014, 7, 3

The columns "Start_Time" and "End_Time" are pandas Series. I want to convert the datatype of both columns to datetime instead.

I need those two columns to have datetime datatypes because in a later block of code, I say if this column is between this date and this date then I label it Quarter 1.

*Any help is greatly appreciated

1
  • Um, I'm fairly certain that the datatype is not series. How do you figure they are? Why don't you post the output of df.head() and df.dtypes. I suspect your columns have object dtypes. Commented Jan 3, 2017 at 18:08

1 Answer 1

1

You can use to_datetime() with a format string to extract the date:

date = pd.to_datetime(df.Start_Time, format='%Y, %m, %d').dt.date

You can also modify the dates in place:

df[['Start_Time', 'End_Time']] = df[['Start_Time', 'End_Time']].apply(
    lambda x: pd.to_datetime(x, format='%Y, %m, %d').dt.date)

Or, you can convert to date when reading the csv:

to_date = lambda x: pd.to_datetime(x, format='%Y, %m, %d').date()
converters = dict(Start_Time=to_date, End_Time=to_date)
df = pd.read_csv(StringIO(data), converters=converters)

A testable example:

import pandas as pd
from io import StringIO

data = u"""
Day_Part,Start_Time,End_Time
"Quarter 1","2014, 1, 1","2015, 3, 1"
"Quarter 2","2014, 3, 3","2014, 7, 3"
"""
df = pd.read_csv(StringIO(data))

# You can use `to_datetime()` with a format string to extract the date:
date = pd.to_datetime(df.Start_Time, format='%Y, %m, %d').dt.date

# The start month in the second row is 3
assert date[1].month == 3

# You can also modify in place
df[['Start_Time', 'End_Time']] = df[['Start_Time', 'End_Time']].apply(
    lambda x: pd.to_datetime(x, format='%Y, %m, %d').dt.date)

# The end month in the second row is 7
assert df.End_Time[1].month == 7

# You can convert to date when reading the csv
to_date = lambda x: pd.to_datetime(x, format='%Y, %m, %d').date()
converters = dict(Start_Time=to_date, End_Time=to_date)
df = pd.read_csv(StringIO(data), converters=converters)

# The end month in the first row is 3
assert df.End_Time[0].month == 3
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.