Data Science Jumpstart with 10 Projects Transcripts
Chapter: Project 6: Working with Time Series - Air Quality over Time
Lecture: Parsing Dates Using Format Strings and to_datetime

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Okay, I'm going to show you how to do some date parsing now. Oftentimes when you load a CSV file, its dates will be strings and we want to convert them
0:10 into dates. So let's go back to where we were from our last section here. You can see that we've got a date column and we've got a time column.
0:21 So I'm going to make a new column called Date and it's going to be a lambda.
0:27 It's going to take my date column, add a space to it and then add the time column and the time column is going to replace the periods with colons.
0:37 Let's scroll over to the end. We should see that new date column. And I'll just pull those out. You can see them this way.
0:47 Okay, so let's see if we can convert that to a date time. There is a PD2 date time function in pandas. Let's run that.
0:58 And when we run that, we get an error. Let's look at our problem here. It gives us a semi-useful message here.
1:04 It says that this format 13032004 doesn't match month, day, year. And it looks like our issue is month and days are in the wrong order there.
1:19 Pandas is trying to do US-centric date parsing. This isn't US-centric. Let's come in here and say day first is true and run it again.
1:28 And that looks like that does work. Alternatively, we could specify the format explicitly. Let's try doing that. We're going to say %d is the format.
1:42 That looks like that works as well. We could probably use this and instead of replacing the periods with question marks,
1:49 we could just stick in periods there. That should probably work as well. Let's look at our D types now.
2:00 And now we have a date time 64ns down here for our date column.


Talk Python's Mastodon Michael Kennedy's Mastodon