PythonDataScienceHandbook icon indicating copy to clipboard operation
PythonDataScienceHandbook copied to clipboard

Bug in Chapter 3.01

Open pglushko opened this issue 11 months ago • 12 comments

Error is located in "Index as ordered set" section

indA = pd.Index([1, 3, 5, 7, 9])
indB = pd.Index([2, 3, 5, 7, 11])

indA & indB # intersection returns Index([0, 3, 5, 7, 9], dtype='int64')

but for intersection result should be [3, 5, 7], instead pandas performs logical AND

indA.intersection(indB) # Index([3, 5, 7], dtype='int64')

P.S. I can open PR, but then section would look more organically if for examples object methods would be presented, with comment that boolean ops are also supported

pglushko avatar Dec 30 '25 21:12 pglushko

Thanks for pointing this out. Adding a brief explanation of why indA & indB behaves differently from indA.intersection(indB) would definitely make the example clearer and help avoid confusion for readers.

On Wed, Aug 5, 2026 at 6:12 AM karlhegci @.***> wrote:

karlhegci left a comment (jakevdp/PythonDataScienceHandbook#443) https://github.com/jakevdp/PythonDataScienceHandbook/issues/443#issuecomment-5187443269

The example should clarify that indA & indB returns an incorrect result because it performs a logical AND operation, while indA.intersection(indB) gives the expected intersection output of [3, 5, 7]; including a note about this distinction would improve clarity.

— Reply to this email directly, view it on GitHub https://github.com/jakevdp/PythonDataScienceHandbook/issues/443?email_source=notifications&email_token=CKTITMGU2YRGKPLX3HXOV2L5IKXZXA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMJYG42DIMZSGY42M4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSWGM33PORSXEX3DNRUWG2Y#issuecomment-5187443269, or unsubscribe https://github.com/notifications/unsubscribe-auth/CKTITMH3O2NCDRRIFY5QQJD5IKXZXAVCNFSNUABEKJSXA33TNF2G64TZHM3DKMZYHA4TCNZ3JFZXG5LFHMZTONZRGMZTGMBYHCQXMAQ . Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS https://github.com/notifications/mobile/ios/CKTITMDUWI6JGSRAKHYEQCT5IKXZXA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMJYG42DIMZSGY42M4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSVGM33PORSXEX3JN5ZQ and Android https://github.com/notifications/mobile/android/CKTITMBFNC4QXS4Y6BCSUBL5IKXZXA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMJYG42DIMZSGY42M4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSXGM33PORSXEX3BNZSHE33JMQ. Download it today! You are receiving this because you are subscribed to this thread.Message ID: @.***>

wafir645fcqs49 avatar Aug 05 '26 08:08 wafir645fcqs49

Skip to content https://mail.google.com/mail/u/0/ Using Gmail with screen readers https://mail.google.com/mail/u/0/ https://accounts.google.com/SignOutOptions?hl=en&continue=https://mail.google.com/mail&service=mail&ec=GBRAFw Enable desktop notifications for Gmail. OK No thanks 2 of 19 Re: [jakevdp/PythonDataScienceHandbook] Bug in Chapter 3.01 (Issue #443) Inbox Summarize this email wafir645fcqs49 @.***> Wed, Aug 5, 10:26 AM (5 days ago) to jakevdp/PythonDataScienceHandbook, Subscribed wafir645fcqs49 left a comment (jakevdp/PythonDataScienceHandbook#443) https://github.com/jakevdp/PythonDataScienceHandbook/issues/443#issuecomment-5189408090 Thanks for pointing this out. Adding a brief explanation of why indA & indB behaves differently from indA.intersection(indB) would definitely make the example clearer and help avoid confusion for readers.

On Wed, Aug 5, 2026 at 6:12 AM karlhegci @.***> wrote:

karlhegci left a comment (jakevdp/PythonDataScienceHandbook#443) < https://github.com/jakevdp/PythonDataScienceHandbook/issues/443#issuecomment-5187443269

The example should clarify that indA & indB returns an incorrect result because it performs a logical AND operation, while indA.intersection(indB) gives the expected intersection output of [3, 5, 7]; including a note about this distinction would improve clarity.

— Reply to this email directly, view it on GitHub < https://github.com/jakevdp/PythonDataScienceHandbook/issues/443?email_source=notifications&email_token=CKTITMGU2YRGKPLX3HXOV2L5IKXZXA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMJYG42DIMZSGY42M4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSWGM33PORSXEX3DNRUWG2Y#issuecomment-5187443269 , or unsubscribe < https://github.com/notifications/unsubscribe-auth/CKTITMH3O2NCDRRIFY5QQJD5IKXZXAVCNFSNUABEKJSXA33TNF2G64TZHM3DKMZYHA4TCNZ3JFZXG5LFHMZTONZRGMZTGMBYHCQXMAQ

. Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS < https://github.com/notifications/mobile/ios/CKTITMDUWI6JGSRAKHYEQCT5IKXZXA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMJYG42DIMZSGY42M4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSVGM33PORSXEX3JN5ZQ

and Android < https://github.com/notifications/mobile/android/CKTITMBFNC4QXS4Y6BCSUBL5IKXZXA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMJYG42DIMZSGY42M4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSXGM33PORSXEX3BNZSHE33JMQ . Download it today! You are receiving this because you are subscribed to this thread.Message ID: @.***>

— Reply to this email directly, view it on GitHub https://github.com/jakevdp/PythonDataScienceHandbook/issues/443?email_source=notifications&email_token=CI5ETW5R3JZEMX3Z74DBS735ILVRZA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMJYHE2DAOBQHEYKM4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSWGM33PORSXEX3DNRUWG2Y#issuecomment-5189408090, or unsubscribe https://github.com/notifications/unsubscribe-auth/CI5ETWZOQJISIV644GVX4CT5ILVRZAVCNFSNUABEKJSXA33TNF2G64TZHM3DKMZYHA4TCNZ3JFZXG5LFHMZTONZRGMZTGMBYHCQXMAQ . Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS https://github.com/notifications/mobile/ios/CI5ETW4GCJY7RIFFGSMH7YT5ILVRZA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMJYHE2DAOBQHEYKM4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSVGM33PORSXEX3JN5ZQ and Android https://github.com/notifications/mobile/android/CI5ETW3YTKH7ZIL7JJ7IOUT5ILVRZA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMJYHE2DAOBQHEYKM4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSXGM33PORSXEX3BNZSHE33JMQ. Download it today! You are receiving this because you are subscribed to this thread.

On Wed, Aug 5, 2026 at 10:26 AM wafir645fcqs49 @.***> wrote:

wafir645fcqs49 left a comment (jakevdp/PythonDataScienceHandbook#443) https://github.com/jakevdp/PythonDataScienceHandbook/issues/443#issuecomment-5189408090 Thanks for pointing this out. Adding a brief explanation of why indA & indB behaves differently from indA.intersection(indB) would definitely make the example clearer and help avoid confusion for readers.

On Wed, Aug 5, 2026 at 6:12 AM karlhegci @.***> wrote:

karlhegci left a comment (jakevdp/PythonDataScienceHandbook#443) < https://github.com/jakevdp/PythonDataScienceHandbook/issues/443#issuecomment-5187443269>

The example should clarify that indA & indB returns an incorrect result because it performs a logical AND operation, while indA.intersection(indB) gives the expected intersection output of [3, 5, 7]; including a note about this distinction would improve clarity.

— Reply to this email directly, view it on GitHub < https://github.com/jakevdp/PythonDataScienceHandbook/issues/443?email_source=notifications&email_token=CKTITMGU2YRGKPLX3HXOV2L5IKXZXA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMJYG42DIMZSGY42M4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSWGM33PORSXEX3DNRUWG2Y#issuecomment-5187443269>,

or unsubscribe < https://github.com/notifications/unsubscribe-auth/CKTITMH3O2NCDRRIFY5QQJD5IKXZXAVCNFSNUABEKJSXA33TNF2G64TZHM3DKMZYHA4TCNZ3JFZXG5LFHMZTONZRGMZTGMBYHCQXMAQ>

. Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS < https://github.com/notifications/mobile/ios/CKTITMDUWI6JGSRAKHYEQCT5IKXZXA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMJYG42DIMZSGY42M4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSVGM33PORSXEX3JN5ZQ>

and Android < https://github.com/notifications/mobile/android/CKTITMBFNC4QXS4Y6BCSUBL5IKXZXA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMJYG42DIMZSGY42M4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSXGM33PORSXEX3BNZSHE33JMQ>.

Download it today! You are receiving this because you are subscribed to this thread.Message ID: @.***>

— Reply to this email directly, view it on GitHub https://github.com/jakevdp/PythonDataScienceHandbook/issues/443?email_source=notifications&email_token=CI5ETW5R3JZEMX3Z74DBS735ILVRZA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMJYHE2DAOBQHEYKM4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSWGM33PORSXEX3DNRUWG2Y#issuecomment-5189408090, or unsubscribe https://github.com/notifications/unsubscribe-auth/CI5ETWZOQJISIV644GVX4CT5ILVRZAVCNFSNUABEKJSXA33TNF2G64TZHM3DKMZYHA4TCNZ3JFZXG5LFHMZTONZRGMZTGMBYHCQXMAQ . Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS https://github.com/notifications/mobile/ios/CI5ETW4GCJY7RIFFGSMH7YT5ILVRZA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMJYHE2DAOBQHEYKM4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSVGM33PORSXEX3JN5ZQ and Android https://github.com/notifications/mobile/android/CI5ETW3YTKH7ZIL7JJ7IOUT5ILVRZA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMJYHE2DAOBQHEYKM4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSXGM33PORSXEX3BNZSHE33JMQ. Download it today! You are receiving this because you are subscribed to this thread.Message ID: @.***>

bsoua8745s12 avatar Aug 10 '26 09:08 bsoua8745s12

Thanks for pointing this out. Clarifying the difference between the logical AND operation and indA.intersection(indB) would make the example much clearer.

On Wed, Aug 5, 2026 at 6:12 AM karlhegci @.***> wrote:

karlhegci left a comment (jakevdp/PythonDataScienceHandbook#443) https://github.com/jakevdp/PythonDataScienceHandbook/issues/443#issuecomment-5187443269

The example should clarify that indA & indB returns an incorrect result because it performs a logical AND operation, while indA.intersection(indB) gives the expected intersection output of [3, 5, 7]; including a note about this distinction would improve clarity.

— Reply to this email directly, view it on GitHub https://github.com/jakevdp/PythonDataScienceHandbook/issues/443?email_source=notifications&email_token=CI5ETW2N7H7U4ECM72KBJ735IKXZVA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMJYG42DIMZSGY42M4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSWGM33PORSXEX3DNRUWG2Y#issuecomment-5187443269, or unsubscribe https://github.com/notifications/unsubscribe-auth/CI5ETWZ3DYEMU6WBGDYUPQT5IKXZVAVCNFSNUABEKJSXA33TNF2G64TZHM3DKMZYHA4TCNZ3JFZXG5LFHMZTONZRGMZTGMBYHCQXMAQ . Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS https://github.com/notifications/mobile/ios/CI5ETW5Z3BLYSW67LMD77PL5IKXZVA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMJYG42DIMZSGY42M4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSVGM33PORSXEX3JN5ZQ and Android https://github.com/notifications/mobile/android/CI5ETW527VALY6TPOTW6W3L5IKXZVA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMJYG42DIMZSGY42M4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSXGM33PORSXEX3BNZSHE33JMQ. Download it today! You are receiving this because you are subscribed to this thread.Message ID: @.***>

bsoua8745s12 avatar Aug 16 '26 09:08 bsoua8745s12

The core problem in the "Index as ordered set" section is that indA & indB ends up returning Index([0, 3, 5, 7, 9], dtype='int64') for the listed values instead of performing the intersection. Showing indA.intersection(indB) as the primary way to get [3, 5, 7] would resolve this directly.

gustavocastillo5 avatar Sep 02 '26 17:09 gustavocastillo5

The & operator is currently performing a bitwise AND on the integer values rather than a set intersection because pandas treats these indices as numeric arrays rather than sets. Updating the text to prioritize indA.intersection(indB) is the right move, but we should also explicitly warn readers that & behavior on pd.Index objects depends heavily on the underlying dtype.

UfBt12qJNZKcw avatar Sep 11 '26 20:09 UfBt12qJNZKcw

This behavior changed in pandas 1.0.0 where the & operator on integer Index objects was deprecated for

jadayaanaliah26 avatar Sep 14 '26 11:09 jadayaanaliah26

Since the & operator behavior on pandas indices depends on the underlying dtype and often defaults to bitwise operations, we should explicitly document that intersection is the only reliable method for set operations to avoid these silent numeric misinterpretations.

heenll68 avatar Sep 27 '26 16:09 heenll68

Since bitwise & on Index objects behaves as an element-wise operation rather than set intersection, we should replace the current example with indA.intersection(indB) and add a note that the & operator is reserved for bitwise logic in pandas.

dredonjaquana26 avatar Oct 15 '26 22:10 dredonjaquana26

To clarify the behavior for readers, we should note that indA & indB is executing a bitwise operation on the underlying int64 data, which is why the output reflects index-wise matching rather than set intersection. I suggest adding a sidebar or note explaining that while the bitwise operators are supported, they do not behave as set operators on numeric Index objects. We could also include a snippet demonstrating indA.intersection(indB) alongside the current example to explicitly show the expected set output of Index([3, 5, 7]).

cherryrodney26 avatar Oct 19 '26 03:10 cherryrodney26

The bitwise & operator behaves this way because pandas defaults to element-wise logical operations on numeric Index objects, which explains the unexpected return of [0, 3, 5, 7, 9]. To prevent further confusion, we should update the text to explicitly contrast this behavior with indA.intersection(indB) and provide a code snippet showing that the intersection method works consistently regardless of the underlying dtype. Would it be better to add a note explaining that & only acts as a set intersection when the Index objects contain object-type data, or should we strictly steer users toward the method-based approach for all set operations?

Ix3WUhpgTPE avatar Oct 31 '26 23:10 Ix3WUhpgTPE

It's worth noting that pandas deprecated & and | for set operations on `Index

Bbk1AtJJF8 avatar Nov 12 '26 15:11 Bbk1AtJJF8