How do I work with dates and times in pandas?
Key Takeaways
The video demonstrates how to work with dates and times in pandas, including converting data to datetime format, accessing datetime attributes, and performing datetime comparisons and mathematical operations.
Full Transcript
hello and welcome back to my QA video series about the pandas library in Python and the question for today is how do I work with dates and times in pandas okay great question there's a lot of powerful time series functionality in pandas and in fact a pandas series is named after the time series so I'm just going to show you the basics today okay so we're going to start by importing pandas as PD and then our example data set will be UFO reports so PD read CSV I need that as a string and bitly slash UFO reports okay and let's take a look at the head all right so each row represents a UFO reported sighting and what if I wanted to analyze the sightings by year or by time of day how would I do that so let's take a look at the D types and check those out and we'll see that the time column is an object which in this case means it's stored as a string so if I wanted to analyze the hour for example I might think well I could do some string slicing okay so let's try like UFO time dot stir dot slice and we'll slice from position negative five so five characters from the end to negative three which is three characters from the end okay and that does work it outputs a string and we probably want it like this is a two character string we probably want as an integer so we'll do dot as type int animal thrown head and now we've got the hour and this works in this case but this feels like an approach that would easily break it's very brittle okay so let's use a better methodology Kai and the solution here is to convert the time column two pandas special date-time format okay so I'm going to overwrite the time column and I'm going to say UFO bracket time equals PD to date/time this is a top level function and I'm going to pass it UFO dot time okay and if we look at the head we'll see that the data seems to be the same as before the formatting looks like it's different but the real thing that's changed is the D type is now date time kinds of special date-time format now before we move on I want to point out I did not have to specify two pandas with this date time function the format of this I did not have to tell it this is a month then there's a slash character and then there's a day then there's a four-digit year then there's a space etc it just figured it out and it and it did that automatically now if you try this on your own data and it does not work automatically there are a lot of options you can use with PD to daytime in order to get it to work okay so what are some benefits that using the date-time format gives us well the big one is it exposes some really convenient attributes like UFO time dot DT so this is a little DT namespace dot our okay that pulls out the hour for us or dot weekday name it understand it actually knows that June 1st 1930 was a Sunday you don't have to write custom code if you're for instance analyzing something by day of the week and there's also just a number version of the weekday ok you can do you can do other things like day of year and it will tell us that June 1st 1930 was the 150 second day of that year ok so there's a ton of attributes like this if you want to see them all go google pandas api reference get to this page and then search for dot DT dot and you'll click on date time like properties and you'll see all of these series properties under series DT dot whatever okay so um let me show you a couple more things and let's do dot heads so this is not taking over the screen and um let's pass PD dot to date time uh date time let's pass it a string instead of a series and I'm just going to say one one 19.99 okay it outputs what's known as a timestamp um and one thing I want to point out is note that I change the format of how I passed I passed it month/day/year in my case and it figured it out um I did not have to specify uh you know that this was the month this was the day this was the year ok so this is called a timestamp and we're going to save this as TS and the reason I save that is I'm going to use that in a comparison so one trick you can do with timestamps is you can use them as part of comparison so if I say you fo dot Lok I can say UFO dot time greater than or equal to time stamp : so I'm saying which Rose do I want to see in the UFO data frame I want to see the ones where the time is greater than or equal to this time stamp and because UFO time is date time and because T s is a time stamp it can do this comparison with greater than or equal to and then the : just means show me all columns so we'll run that and we'll see that it's only showing us rows in which the time is greater than meaning into the future from January 1st 1999 okay pretty cool all right let me throw that head on here and another trick I want to show you is that you can do kind of like mathematical operations with the date-time format so if I say UFO time max it will tell me the latest timestamp in the time series okay I can even say UFO time dot max - UFO time min so you can do math with these and it tells you it outputs in a special object called a time delta object and it tells you that the difference between the earliest row and the latest row is twenty-five thousand seven hundred eighty one days etc okay so this is called a time delta object and time delta objects also have attributes like dot days and you can pull things out like that so you can get really fancy with the stuff this is really just the basics as always I'm going to end with a bonus and the bonus for today is I'm going to do a little plotting of the number of UFO reports by year okay so to plot in the Jupiter notebook we need to do plot live in line okay that allows plots to appear and then I'm going to store I'm going to create a new column called UFO year and I'm actually just going to store UFO time dot DT dot year in it okay and we can see that that worked you go for that head okay so we've got this year column alright now if I wanted if I want to analyze how many reported sightings by year one way to do it it's not the only way would be UFO year dot value counts how many rows how many times does each year appear well this is great except it's sorted by value arm so let's sort it by the index instead so dot sort index okay and now it's in the order of the index and all we have to do to get a plot is to just say dot plot and it does a line plot by default and here we go here is our plot of UFO reported sightings by ear okay so that's it for today thank you so much for joining me as always please click Subscribe if you'd like to see more videos like this please leave me a comment below if you have a comment for me or a question I'd love to hear from you but that's it so I hope to see you again soon
Original Description
Let's say that you have dates and times in your DataFrame and you want to analyze your data by minute, month, or year. What should you do? In this video, I'll demonstrate how you can convert your data to "datetime" format, enabling you to access a ton of convenient attributes and perform datetime comparisons and mathematical operations.
SUBSCRIBE to learn data science with Python:
https://www.youtube.com/dataschool?sub_confirmation=1
JOIN the "Data School Insiders" community and receive exclusive rewards:
https://www.patreon.com/dataschool
== RESOURCES ==
GitHub repository for the series: https://github.com/justmarkham/pandas-videos
"to_datetime" documentation: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_datetime.html
Datetime properties and methods: http://pandas.pydata.org/pandas-docs/stable/api.html#datetimelike-properties
== LET'S CONNECT! ==
Newsletter: https://www.dataschool.io/subscribe/
Twitter: https://twitter.com/justmarkham
Facebook: https://www.facebook.com/DataScienceSchool/
LinkedIn: https://www.linkedin.com/in/justmarkham/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Data School · Data School · 47 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
▶
48
49
50
51
52
53
54
55
56
57
58
59
60
Setting up Git and GitHub
Data School
Navigating a GitHub Repository - Part 1
Data School
Forking a GitHub Repository
Data School
Creating a New GitHub Repository
Data School
Copying a GitHub Repository to Your Local Computer
Data School
Committing Changes in Git and Pushing to a GitHub Repository
Data School
Syncing Your GitHub Fork
Data School
Allstate Purchase Prediction Challenge on Kaggle
Data School
Troubleshooting: Updates Rejected When Pushing to GitHub
Data School
Hands-on dplyr tutorial for faster data manipulation in R
Data School
ROC Curves and Area Under the Curve (AUC) Explained
Data School
Going deeper with dplyr: New features in 0.3 and 0.4 (tutorial)
Data School
What is machine learning, and how does it work?
Data School
Setting up Python for machine learning: scikit-learn and Jupyter Notebook
Data School
Getting started in scikit-learn with the famous iris dataset
Data School
Training a machine learning model with scikit-learn
Data School
Comparing machine learning models in scikit-learn
Data School
Data science in Python: pandas, seaborn, scikit-learn
Data School
Selecting the best model in scikit-learn using cross-validation
Data School
How to find the best model parameters in scikit-learn
Data School
How to evaluate a classifier in scikit-learn
Data School
What is pandas? (Introduction to the Q&A series)
Data School
How do I read a tabular data file into pandas?
Data School
How do I select a pandas Series from a DataFrame?
Data School
Why do some pandas commands end with parentheses (and others don't)?
Data School
How do I rename columns in a pandas DataFrame?
Data School
How do I remove columns from a pandas DataFrame?
Data School
How do I sort a pandas DataFrame or a Series?
Data School
How do I filter rows of a pandas DataFrame by column value?
Data School
How do I apply multiple filter criteria to a pandas DataFrame?
Data School
Your pandas questions answered!
Data School
How do I use the "axis" parameter in pandas?
Data School
How do I use string methods in pandas?
Data School
How do I change the data type of a pandas Series?
Data School
When should I use a "groupby" in pandas?
Data School
How do I explore a pandas Series?
Data School
How do I handle missing values in pandas?
Data School
What do I need to know about the pandas index? (Part 1)
Data School
What do I need to know about the pandas index? (Part 2)
Data School
How do I select multiple rows and columns from a pandas DataFrame?
Data School
Machine Learning with Text in scikit-learn (PyCon 2016)
Data School
When should I use the "inplace" parameter in pandas?
Data School
How do I make my pandas DataFrame smaller and faster?
Data School
How do I use pandas with scikit-learn to create Kaggle submissions?
Data School
More of your pandas questions answered!
Data School
How do I create dummy variables in pandas?
Data School
How do I work with dates and times in pandas?
Data School
How do I find and remove duplicate rows in pandas?
Data School
How do I avoid a SettingWithCopyWarning in pandas?
Data School
How do I change display options in pandas?
Data School
How do I create a pandas DataFrame from another object?
Data School
How do I apply a function to a pandas Series or DataFrame?
Data School
Getting started with machine learning in Python (webcast)
Data School
Q&A about Machine Learning with Text (online course)
Data School
Your pandas questions answered! (webcast)
Data School
Machine Learning with Text in scikit-learn (PyData DC 2016)
Data School
Write Pythonic Code for Better Data Science (webcast)
Data School
Web scraping in Python (Part 1): Getting started
Data School
Web scraping in Python (Part 2): Parsing HTML with Beautiful Soup
Data School
Web scraping in Python (Part 3): Building a dataset
Data School
More on: ML Pipelines
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
The AI Moat Paradox: The Better Models Become, the Less Models Matter
Medium · AI
170,927 AI Papers Reveal the Biggest Research Shifts of the First Half of 2026
Medium · Machine Learning
170,927 AI Papers Reveal the Biggest Research Shifts of the First Half of 2026
Medium · Data Science
[PoV] When Everyone Is Smart, No One Is
Medium · AI
🎓
Tutor Explanation
DeepCamp AI