Chapter 1 ยท Section 3 practice

Selection and Index Alignment

0 of 4 answered

These questions review the examples in the companion slides. Choose an answer, then check the explanation.

Question 1

How many rows do aep.loc[โ€œ2017-01-01 01:00โ€:โ€œ2017-01-01 05:00โ€] and aep.iloc[0:5] return?

  • 4 and 5
  • 5 and 5
  • 5 and 4
  • 4 and 4

Question 2

For jan = sp500.loc[โ€œ2020โ€], do jan.loc[โ€œ2020-01-06โ€:โ€œ2020-01-08โ€] and jan.iloc[2:5] return the same rows?

  • Yes โ€” Jan 6, 7, 8 both times, but for opposite slicing rules
  • No โ€” .loc gives 2 rows, .iloc gives 3
  • No โ€” .iloc includes Jan 9
  • They raise โ€” you cannot mix index value and position

Question 3

r.loc[r > 0.02 & r < 0.05] โ€ฆ

  • Works โ€” same as the parenthesised version
  • Returns an empty Series
  • Raises TypeError โ€” & binds tighter than >
  • Returns the days above 2 % only

Question 4

price.sample(500, replace=True, random_state=0) โ€ฆ

  • Always returns the first 500 rows
  • Returns 500 distinct rows
  • May contain the same product twice
  • Raises unless frac is given