Issues with dates and time in Python

Viewed 22

I need to add 12 hours to dropoff_datetime column for all the trips with negative duration.

This is the prompt I was given: Use where function with three arguments: within the condition compare values of df['duration'] with timedelta(0) object. inplace should be set to True, and other argument need to be set to result of addition dropoff_datetime column and timedelta object with 12 hours.

Below is the code I have written, but the output still seems to come back incorrect. I think my "other" statement is the issue.

# Load libraries
import pandas as pd
from datetime import timedelta

# Loading dataset, creating duration column, and filtering to negative durations
url = 'https://drive.google.com/uc?id=1YV5bKobzYxVAWyB7VlxNH6dmfP4tHBui'
df = pd.read_csv(url, parse_dates = ['pickup_datetime', 'dropoff_datetime', 'dropoff_calculated'])
df["duration"] = pd.to_timedelta(df["duration"])

# Task 1: add 12 hours to dropoff duration for negative durations
df['duration'].where(~(df['duration'] < timedelta(0)), other = df['dropoff_datetime'] + timedelta(12), inplace = True)

# Task 2: recalculate duration column
df['duration'] = df['dropoff_datetime'] - df['pickup_datetime']

# Task 3: inspect first 10 rows with negative duration
print(df[df['duration'] < timedelta(0)][["pickup_datetime", "dropoff_datetime", "trip_duration", "dropoff_calculated"]].head(5))

Output:

    pickup_datetime        dropoff_datetime       trip_duration  \

34 2016-09-19 11:47:23 2016-09-19 02:21:19 0 days 02:33:56
66 2016-09-20 12:11:43 2016-09-20 02:15:55 0 days 02:04:13
74 2016-09-20 12:55:00 2016-09-20 01:03:36 0 days 00:08:36
132 2017-04-22 12:38:41 2017-04-22 01:20:13 0 days 00:41:32
231 2017-04-24 12:56:31 2017-04-24 01:06:18 0 days 00:09:47

    dropoff_calculated  

34 2016-09-19 14:21:19
66 2016-09-20 14:15:56
74 2016-09-20 13:03:36
132 2017-04-22 13:20:13
231 2017-04-24 13:06:18

1 Answers

Two things: (1) Task 2 is overwriting what you're doing in Task 1 with the where command, so that has to be removed, and (2) you had the wrong column in the where command, should be duration. With those 2 small changes I believe your code works as expected:

# Load libraries
import pandas as pd
from datetime import timedelta

# Loading dataset, creating duration column, and filtering to negative durations
url = 'https://drive.google.com/uc?id=1YV5bKobzYxVAWyB7VlxNH6dmfP4tHBui'
df = pd.read_csv(url, parse_dates = ['pickup_datetime', 'dropoff_datetime', 'dropoff_calculated'])
df["duration"] = pd.to_timedelta(df["duration"])

# Task 1: add 12 hours to dropoff duration for negative durations
df['duration'].where(~(df['duration'] < timedelta(0)), other = df['duration'] + timedelta(12), inplace = True)

# Task 2: recalculate duration column
# df['duration'] = df['dropoff_datetime'] - df['pickup_datetime']

# Task 3: inspect first 10 rows with negative duration
print(df[df['duration'] < timedelta(0)][["pickup_datetime", "dropoff_datetime", "trip_duration", "dropoff_calculated"]].head(5))

Outputs:

Empty DataFrame
Columns: [pickup_datetime, dropoff_datetime, trip_duration, dropoff_calculated]
Index: []
Related