143-5 solution syntax not previewed or taught? chained indexing?

I think a kind of code that wasn’t taught was used in an exercise’s solution.
If that’s the case that would be frustrating.

Also, after too many hours of searching for answers about it, I think the kind of code that was used in the solution is often discouraged (and that might be why I had such a hard time finding any documentation explaining it).

I hope my trouble has some use, maybe for DQ in reviewing their teaching materials. I’d be glad to be corrected about anything I’ve thought.

Thanks for your work.

Screen Link: https://app.dataquest.io/m/143/multiple-plots/5/adding-data

The solution code in question on that step 5 of mission 143:

fig = plt.figure()
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)

ax1.plot(unrate[0:12]['DATE'], unrate[0:12]['VALUE'])        # chained indexing 
ax2.plot(unrate[12:24]['DATE'], unrate[12:24]['VALUE'])    # here too?

plt.show()

I’m still practicing getting better at searching for explanations, including trying to get something out of official documentation for code sets like pandas or numpy. I went to the pandas site, eventually hit the user guide, saw “indexing and selecting data” as the second subheading on the left margin, then browsed the whole very long page, looking for something that matched the kind of indexing I saw in the solution for 143-5.

I found out about “chained indexing” at the bottom of the page, in the last subheading there, which you can get to by clicking on the last subheading on the right margin of the page, or going straight to:

https://pandas.pydata.org/docs/user_guide/indexing.html#returning-a-view-versus-a-copy

Anyway, it feels good to share my progress in difficult things, so thanks, again, for reading all this.

Hey, Davis.

Thank you very much for this feedback. We appreciate it a lot, we always are on the lookout for things that learners find confusing.

I disagree that this is new syntax, though. You know about the syntax DataFrame[column_name]. Well, unrate[:12] is a dataframe, say, df. So you can use the notation df[column_name]. Replacing df with what it is (unrate[:12]), you get unrate[:12][column_name].

Let me know if you disagree or if you have further questions.

1 Like

I get how the code sets you wrote are equivalent.

I might have a poor understanding of what “syntax” is, but I think the difference in the code sets is is sort of like the difference between “can not” and “cannot.” A new English speaker might not have seen the latter or be aware that its allowed, but they might still get what it means.

The two would be semantically equivalent, I think, but would have a different syntax–one that doesn’t apply in general (“do” and “donot”). In the code case, for a reason I only browsed and don’t understand, there’s apparently a possible problem that can be caused by “chained indexing” in some cases–maybe not even this one, but it can arise, due to that arrangement (a distinct syntax).

I hope that makes my concern more clear.

Thanks a lot for thinking about it. I’ll try to not get so stuck for so long on something ultimately so much of a footnote, from what I’ve read. I am trying to learn to navigate documentation, but I have to learn to ask for help more quickly–even about how to find help, which is when I really want to curse computers and try carpentry (where I can at least bang the door or table I’m ■■■■■■■■ up making).

That word that’s blocked out is literally what a carpenter does! ■■■■■■■■! Sorry?

Hahahaha. I didn’t even know we had such censorship in place.

It might be me who has a poor understanding of what syntax is :slight_smile:

Chain indexing can definitely lead to problems! But they are problems on a level that doesn’t have to do with us teaching or not teaching this syntax.

I disagree with your example. I think the analogy is good, but the way I see it it fails even in English. Sure, can not and cannot are syntically different (because the fourth character of each differs), but the way in which you use either version is the same, you’re allowed to replace one for the other.

This is what I mean when I say the syntax is the same, I’m referring to what follows unrate[:12].

If you were to write the code below, the syntax would be very, very different, but regarding the [column_name] part, it is the same; that’s what I’m trying to address.

df = unrate[:12]
print(df[column_name])

A simpler example is this:

Version 1

>>> x=3
>>> x+2
5

Version 2

>>> 3+2
5

For sure the overall syntax is different, one has three lines, the other two. But with regards to the actual sum, I’m claiming it is the same. You’re allowed to replace x with what it is.

I’m also claiming — and I could be wrong — that this is another version of your question. It’s like you’re asking why you can do 3+2 given that you only learned about summing variables and numbers.

1 Like