Back to PANDAS FOR DATA ANALYTICS

Working with Date & Time

Learn pandas’ datetime features to parse dates, extract components, and work with time-series data.

10 minutesVideo LessonPDF notes
🎯 Free Guest Mode: You are learning for free. Sign in to save your completion progress and quiz answers.

Ready to continue?

Mark this lesson as complete when you're ready to proceed.

Key moments

  1. Introduction to Time Series — The lesson introduces the powerful time series functionality available in the Pandas library.
  2. String Slicing Pitfall — The instructor demonstrates using string slicing to extract the hour, noting that this approach is brittle and easily broken.
  3. Converting to Datetime — The 'time' column is converted using `pd.to_datetime()`, changing the column's data type to `datetime64`.
  4. Using the .dt Accessor — The `.dt` namespace is used to easily extract attributes like `.hour`, `.weekday_name`, and `.dayofyear` from the datetime Series.
  5. Creating a Timestamp — A single date string is converted into a `Timestamp` object for use in subsequent comparisons.
  6. Datetime Comparisons — The DataFrame is filtered using a comparison operator between the datetime Series and the newly created Timestamp object.
  7. Timedelta Calculation — Subtracting the minimum time from the maximum time yields a `Timedelta` object representing the total duration of the dataset.
  8. Plotting Time Series Data — The year is extracted, value counts are calculated, the index is sorted, and the results are plotted chronologically.
PDF notes

Frequently asked questions

What if `pd.to_datetime()` doesn't automatically figure out my date format?

The `pd.to_datetime()` function accepts a `format` argument where you can explicitly specify the expected date string structure using standard format codes.

What is the difference between a Timestamp and a Timedelta?

A Timestamp represents a specific point in time (e.g., June 1, 1930). A Timedelta represents a duration or difference between two points in time (e.g., 25,781 days).

Why is string slicing considered brittle for date extraction?

String slicing relies on fixed character positions; if the source format changes, the slice will extract the wrong characters.

How do I see all available attributes under the `.dt` accessor?

Consult the official Pandas API reference documentation for the Series `.dt` properties and methods to see all available extraction options.

How was this lesson?

Your feedback helps us refine explanations and catch bugs.