Move from Excel to Python with Pandas Transcripts
Chapter: Data wrangling with Pandas
Lecture: Pandas' dt, the date time accessor
0:00 Now we're going to read in our sample sales data into our Jupiter notebook. So we'll do the imports. I went ahead and put those in here,
0:07 and now you can see the data frame of that represents the Excel file. And if we do DF info,
0:14 it tells us that the purchase date is a date time 64 data type, which is good, which is what we had expected.
0:21 Quantity, price, extended amount and shipping costs are numeric values. So everything appears to be in order here.
0:29 Here's how we might think about actually accessing the purchase date. So if we know that we have a purchase state,
0:36 maybe we could try typing month after that. And we get an attribute error so Pandas doesn't know how to get at the month
0:45 And so what penance has done is it has introduced a concept of an excess er and D T stands for daytime.
0:53 So now it knows that this is a daytime data type, and there is an excess er called D T, which enables us to get at the underlying data in that column.
1:05 And here we want to pull out the month we can do a similar sort of so year works as expected. And there are some that you may not think of
1:15 what's try like Day of Week Pandas goes in and Comptel, what day of the week each of those days is and assigns a numerical value to
1:25 it. So remember the example we had of trying to get the quarter and how we had to do a fairly, maybe non intuitive calculation for Excel?
1:35 Let's take a look at what if we just use quarter? Ah, so that tells us that Pamela's knows the concept of quarter and can automatically
1:45 calculate that force, which is really helpful. And the recent one highlight This is there are a lot of options available once you
1:53 have the correct data type to make your data manipulation just a little bit easier.
1:57 For instance, what if you want to know whether a current month has 30 or 31? Or maybe it's a leap year.
2:06 We can look at days and month so we can see that it calculates a 31 and 30. We can also see if something is the end of the month.
2:17 So none of these examples that are showing just the head and the tail. But it is a helpful thing to keep in mind as you doom or data manipulation
2:25 Now, one of the things that you really need to keep in mind is that I did all of this. But there's been no underlying change to the data frame.
2:35 If we want to actually add some of these new columns to data frame, we need to make sure that we explicitly do so.
2:49 So what I've done here is I've created two new columns purchase month and purchase year and assigned the month and year to that.
2:57 You can see the data frame now has the purchase month and year. So we are, um, replicating what we had in our Excel spreadsheet and if
3:06 we wanted to add one more to the purchase corner. Now we have our purchase quarter, and you can see that this is March.
3:19 The first quarter in this November,