How do I work with dates and times in pandas?

Data School · Beginner ·📰 AI News & Updates ·9y ago

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 Setting up Git and GitHub
Setting up Git and GitHub
Data School
2 Navigating a GitHub Repository - Part 1
Navigating a GitHub Repository - Part 1
Data School
3 Forking a GitHub Repository
Forking a GitHub Repository
Data School
4 Creating a New GitHub Repository
Creating a New GitHub Repository
Data School
5 Copying a GitHub Repository to Your Local Computer
Copying a GitHub Repository to Your Local Computer
Data School
6 Committing Changes in Git and Pushing to a GitHub Repository
Committing Changes in Git and Pushing to a GitHub Repository
Data School
7 Syncing Your GitHub Fork
Syncing Your GitHub Fork
Data School
8 Allstate Purchase Prediction Challenge on Kaggle
Allstate Purchase Prediction Challenge on Kaggle
Data School
9 Troubleshooting: Updates Rejected When Pushing to GitHub
Troubleshooting: Updates Rejected When Pushing to GitHub
Data School
10 Hands-on dplyr tutorial for faster data manipulation in R
Hands-on dplyr tutorial for faster data manipulation in R
Data School
11 ROC Curves and Area Under the Curve (AUC) Explained
ROC Curves and Area Under the Curve (AUC) Explained
Data School
12 Going deeper with dplyr: New features in 0.3 and 0.4 (tutorial)
Going deeper with dplyr: New features in 0.3 and 0.4 (tutorial)
Data School
13 What is machine learning, and how does it work?
What is machine learning, and how does it work?
Data School
14 Setting up Python for machine learning: scikit-learn and Jupyter Notebook
Setting up Python for machine learning: scikit-learn and Jupyter Notebook
Data School
15 Getting started in scikit-learn with the famous iris dataset
Getting started in scikit-learn with the famous iris dataset
Data School
16 Training a machine learning model with scikit-learn
Training a machine learning model with scikit-learn
Data School
17 Comparing machine learning models in scikit-learn
Comparing machine learning models in scikit-learn
Data School
18 Data science in Python: pandas, seaborn, scikit-learn
Data science in Python: pandas, seaborn, scikit-learn
Data School
19 Selecting the best model in scikit-learn using cross-validation
Selecting the best model in scikit-learn using cross-validation
Data School
20 How to find the best model parameters in scikit-learn
How to find the best model parameters in scikit-learn
Data School
21 How to evaluate a classifier in scikit-learn
How to evaluate a classifier in scikit-learn
Data School
22 What is pandas? (Introduction to the Q&A series)
What is pandas? (Introduction to the Q&A series)
Data School
23 How do I read a tabular data file into pandas?
How do I read a tabular data file into pandas?
Data School
24 How do I select a pandas Series from a DataFrame?
How do I select a pandas Series from a DataFrame?
Data School
25 Why do some pandas commands end with parentheses (and others don't)?
Why do some pandas commands end with parentheses (and others don't)?
Data School
26 How do I rename columns in a pandas DataFrame?
How do I rename columns in a pandas DataFrame?
Data School
27 How do I remove columns from a pandas DataFrame?
How do I remove columns from a pandas DataFrame?
Data School
28 How do I sort a pandas DataFrame or a Series?
How do I sort a pandas DataFrame or a Series?
Data School
29 How do I filter rows of a pandas DataFrame by column value?
How do I filter rows of a pandas DataFrame by column value?
Data School
30 How do I apply multiple filter criteria to a pandas DataFrame?
How do I apply multiple filter criteria to a pandas DataFrame?
Data School
31 Your pandas questions answered!
Your pandas questions answered!
Data School
32 How do I use the "axis" parameter in pandas?
How do I use the "axis" parameter in pandas?
Data School
33 How do I use string methods in pandas?
How do I use string methods in pandas?
Data School
34 How do I change the data type of a pandas Series?
How do I change the data type of a pandas Series?
Data School
35 When should I use a "groupby" in pandas?
When should I use a "groupby" in pandas?
Data School
36 How do I explore a pandas Series?
How do I explore a pandas Series?
Data School
37 How do I handle missing values in pandas?
How do I handle missing values in pandas?
Data School
38 What do I need to know about the pandas index? (Part 1)
What do I need to know about the pandas index? (Part 1)
Data School
39 What do I need to know about the pandas index? (Part 2)
What do I need to know about the pandas index? (Part 2)
Data School
40 How do I select multiple rows and columns from a pandas DataFrame?
How do I select multiple rows and columns from a pandas DataFrame?
Data School
41 Machine Learning with Text in scikit-learn (PyCon 2016)
Machine Learning with Text in scikit-learn (PyCon 2016)
Data School
42 When should I use the "inplace" parameter in pandas?
When should I use the "inplace" parameter in pandas?
Data School
43 How do I make my pandas DataFrame smaller and faster?
How do I make my pandas DataFrame smaller and faster?
Data School
44 How do I use pandas with scikit-learn to create Kaggle submissions?
How do I use pandas with scikit-learn to create Kaggle submissions?
Data School
45 More of your pandas questions answered!
More of your pandas questions answered!
Data School
46 How do I create dummy variables in pandas?
How do I create dummy variables in pandas?
Data School
How do I work with dates and times in pandas?
How do I work with dates and times in pandas?
Data School
48 How do I find and remove duplicate rows in pandas?
How do I find and remove duplicate rows in pandas?
Data School
49 How do I avoid a SettingWithCopyWarning in pandas?
How do I avoid a SettingWithCopyWarning in pandas?
Data School
50 How do I change display options in pandas?
How do I change display options in pandas?
Data School
51 How do I create a pandas DataFrame from another object?
How do I create a pandas DataFrame from another object?
Data School
52 How do I apply a function to a pandas Series or DataFrame?
How do I apply a function to a pandas Series or DataFrame?
Data School
53 Getting started with machine learning in Python (webcast)
Getting started with machine learning in Python (webcast)
Data School
54 Q&A about Machine Learning with Text (online course)
Q&A about Machine Learning with Text (online course)
Data School
55 Your pandas questions answered! (webcast)
Your pandas questions answered! (webcast)
Data School
56 Machine Learning with Text in scikit-learn (PyData DC 2016)
Machine Learning with Text in scikit-learn (PyData DC 2016)
Data School
57 Write Pythonic Code for Better Data Science (webcast)
Write Pythonic Code for Better Data Science (webcast)
Data School
58 Web scraping in Python (Part 1): Getting started
Web scraping in Python (Part 1): Getting started
Data School
59 Web scraping in Python (Part 2): Parsing HTML with Beautiful Soup
Web scraping in Python (Part 2): Parsing HTML with Beautiful Soup
Data School
60 Web scraping in Python (Part 3): Building a dataset
Web scraping in Python (Part 3): Building a dataset
Data School

The video teaches how to work with dates and times in pandas, including converting data to datetime format and accessing datetime attributes. It also demonstrates how to perform datetime comparisons and mathematical operations, and how to plot data using Jupiter notebook.

Key Takeaways
  1. Import pandas and load example data
  2. Convert time column to datetime format using PD to_datetime
  3. Access datetime attributes using DT namespace
  4. Perform datetime comparisons and mathematical operations
  5. Plot data using Jupiter notebook
💡 Converting data to datetime format in pandas allows for easy access to datetime attributes and enables performing datetime comparisons and mathematical operations.

Related AI Lessons

The AI Moat Paradox: The Better Models Become, the Less Models Matter
The AI moat paradox suggests that as AI models improve, their importance may decrease, and understanding this concept is crucial for AI professionals and businesses.
Medium · AI
170,927 AI Papers Reveal the Biggest Research Shifts of the First Half of 2026
Discover the biggest AI research shifts of 2026 based on 170,927 papers, and learn how to apply these trends to your work
Medium · Machine Learning
170,927 AI Papers Reveal the Biggest Research Shifts of the First Half of 2026
Discover the major research shifts in AI from 170,927 papers published in the first half of 2026, and learn how to analyze trends in AI research
Medium · Data Science
[PoV] When Everyone Is Smart, No One Is
In a world where AI makes everyone smart, the value of intelligence decreases, and new challenges arise
Medium · AI
Up next
‘ENOUGH IS ENOUGH’: Lebanon is STANDING UP to Iran, expert says
Fox Business
Watch →