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.
0:03 So we'll do the imports. I went ahead and put those in here,
0:06 and now you can see the data frame of that represents the Excel file.
0:11 And if we do DF info,
0:13 it tells us that the purchase date is a date time 64 data type,
0:17 which is good, which is what we had expected.
0:20 Quantity, price, extended amount and shipping costs are numeric values.
0:25 So everything appears to be in order here.
0:28 Here's how we might think about actually accessing the purchase date.
0:32 So if we know that we have a purchase state,
0:35 maybe we could try typing month after that.
0:39 And we get an attribute error so Pandas doesn't know how to get at the month
0:44 And so what penance has done is it has introduced a concept of an excess
0:49 er and D T stands for daytime.
0:52 So now it knows that this is a daytime data type,
0:58 and there is an excess er called D T,
1:00 which enables us to get at the underlying data in that column.
1:04 And here we want to pull out the month we can do a similar sort of
1:09 so year works as expected. And there are some that you may not think of
1:14 what's try like Day of Week Pandas goes in and Comptel,
1:20 what day of the week each of those days is and assigns a numerical value to
1:24 it. So remember the example we had of trying to get the quarter and how
1:27 we had to do a fairly,
1:29 maybe non intuitive calculation for Excel?
1:34 Let's take a look at what if we just use quarter?
1:38 Ah, so that tells us that Pamela's knows the concept of quarter and can automatically
1:44 calculate that force, which is really helpful.
1:47 And the recent one highlight This is there are a lot of options available once you
1:52 have the correct data type to make your data manipulation just a little bit easier.
1:56 For instance, what if you want to know whether a current month has 30 or
2:02 31? Or maybe it's a leap year.
2:05 We can look at days and month so we can see that it calculates a 31
2:08 and 30. We can also see if something is the end of the month.
2:16 So none of these examples that are showing just the head and the tail.
2:20 But it is a helpful thing to keep in mind as you doom or data manipulation
2:24 Now, one of the things that you really need to keep in mind is
2:28 that I did all of this.
2:30 But there's been no underlying change to the data frame.
2:34 If we want to actually add some of these new columns to data frame,
2:37 we need to make sure that we explicitly do so.
2:48 So what I've done here is I've created two new columns purchase month and purchase year
2:52 and assigned the month and year to that.
2:56 You can see the data frame now has the purchase month and year.
2:59 So we are, um, replicating what we had in our Excel spreadsheet and if
3:05 we wanted to add one more to the purchase corner.
3:14 Now we have our purchase quarter,
3:16 and you can see that this is March.
3:18 The first quarter in this November,