Workspace
Tedilte Abraham/

Working with Dates and Times in Python

0
Beta
Spinner
1. Wrap-up
00:00 - 00:08
In this course, you learned many things about working with dates and times in Python. Let's recap what we've covered.

2. Recap: Dates and Calendars
00:08 - 00:46
In the first chapter of this course, we covered dates in Python. The date() class takes a year, month, and day as arguments. A date object has accessors like year, and also methods like weekday(). date objects can be compared like numbers, using min(), max(), and sort(). You can subtract one date from another to get a timedelta. To turn date objects into strings, use the isoformat() or strftime() methods.

3. Recap: Combining Dates and Times
00:46 - 01:29
In the second chapter of this course, we covered datetimes. The datetime() class takes all the arguments of date(), plus an hour, minute, second, and microsecond. All of the additional arguments are optional; otherwise, they're set to zero by default. You can replace any value in a datetime with the replace() method. Convert a timedelta into an integer with its total_seconds() method. Turn strings into dates with strptime() and dates into strings with strftime().

4. Recap: Timezones and Daylight Saving
01:29 - 02:16
In the third chapter of this course, we covered timezones and daylight saving. A datetime is "timezone aware" when it has its tzinfo set. Otherwise it is "timezone naive". Setting a timezone tells a datetime how to align itself to UTC, the universal time standard. Use the replace() method to change the timezone of a datetime, leaving the date and time the same. Use the astimezone() method to shift the date and time to match the new timezone. dateutil-dot-tz provides a comprehensive, updated timezone database.

5. Recap: Easy and Powerful Timestamps in Pandas
02:16 - 03:13
In the fourth and final chapter of this course, we covered using Pandas for handling dates and times. When reading a csv, set the parse_dates argument to be the list of columns which should be parsed as datetimes. If setting parse_dates doesn't work, use the pd-dot-to_datetime() function. Grouping rows with groupby() lets you calculate aggregates per group. For example, first(), min() or mean(). resample() groups rows on the basis of a datetime column, by year, month, day, and so on. Use tz_localize() to set a timezone, keeping the date and time the same. Use tz_convert() to change the date and time to match a new timezone.

6. Congratulations!
03:13 - 03:35
At this point, you have all of the knowledge you need to effectively use dates and times in Python. Even better, you've had a chance to practice! If you want to get really good with dates, why not get some more practice? Google for "interesting data sets", find one you like that has dates in it, pull it down, and get going!
# Shift the index of the end date up one; now subract it from the start date
rides['Time since'] = rides['Start date'] - (rides['End date'].shift(1))

# Move from a timedelta to a number of seconds, which is easier to work with
rides['Time since'] = rides['Time since'].dt.total_seconds()

# Resample to the month
monthly = rides.resample('M', on='Start date')

# Print the average hours between rides each month
print(monthly['Time since'].mean()/(60*60))
# Add a column for the weekday of the start of the ride
rides['Ride start weekday'] = rides['Start date'].dt.day_name()

# Print the median trip time per weekday
print(rides.groupby('Ride start weekday')['Duration'].median())
# Localize the Start date column to America/New_York
rides['Start date'] = rides['Start date'].dt.tz_localize('America/New_York', 
ambiguous='NaT')

# Print first value
print(rides['Start date'].iloc[0])

# Convert the Start date column to Europe/London
rides['Start date'] = rides['Start date'].dt.tz_convert('Europe/London')

# Print the new value
print(rides['Start date'].iloc[0])
# Localize the Start date column to America/New_York
rides['Start date'] = rides['Start date'].dt.tz_localize('America/New_York', ambiguous='NaT')


# Print first value
print(rides['Start date'].iloc[0])
# Group rides by member type, and resample to the month
grouped = rides.groupby('Member type')\
  .resample('M', on = 'Start date')

# Print the median duration for each group
print(grouped['Duration'].median())
# Resample rides to be monthly on the basis of Start date
monthly_rides = rides.resample('M', on = 'Start date')['Member type']

# Take the ratio of the .value_counts() over the total number of rides
print(monthly_rides.value_counts() / monthly_rides.size())
# Import matplotlib
import matplotlib.pyplot as plt

# Resample rides to monthly, take the size, plot the results
rides.resample("M", on = 'Start date')\
  .size()\
  .plot(ylim = [0, 150])

# Show the results
plt.show()
# Import matplotlib
import matplotlib.pyplot as plt

# Resample rides to daily, take the size, plot the results
rides.resample('D', on = 'Start date')\
  .size()\
  .plot(ylim = [0, 15])

# Show the results
plt.show()
      .format(rides['joyrides'].median('Duration')))
    

Working with Dates and Times in Python

Run the hidden code cell below to import the data used in this course.

Take Notes

Add notes about the concepts you've learned and code cells with code you want to keep.

Add your notes here

# Add your code snippets here



  • AI Chat
  • Code