Skip to content

Commit adc798d

Browse files
dshkolclaude
andcommitted
Fix pandas attrs comparison bug and add labels='short' to tutorial
## Issues Fixed ### 1. Pandas attrs comparison bug causing nlargest() to fail - **Bug**: Storing DataFrame in attrs["census_vectors"] caused pandas to fail when comparing attrs during operations like nlargest() - **Error**: "ValueError: The truth value of a DataFrame is ambiguous" - **Fix**: Store metadata as list of dicts instead of DataFrame in core.py:358 - **Fix**: Update label_vectors() to convert list back to DataFrame in vectors.py:53 ### 2. Missing labels='short' in getting_started tutorial - **Bug**: Tutorial code tried to access 'v_CA21_923' but columns had full labels - **Error**: "KeyError: 'v_CA21_923'" - **Fix**: Added labels='short' parameter in getting_started.md:160 ## Build Results - All tutorials now execute successfully ✅ - No execution errors in documentation build ✅ - Population density calculations work correctly ✅ 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 64aeca7 commit adc798d

3 files changed

Lines changed: 12 additions & 3 deletions

File tree

docs/tutorials/getting_started.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ try:
157157
regions={'CMA': '35535'}, # Toronto CMA
158158
vectors=['v_CA21_923', 'v_CA21_939', 'v_CA21_942', 'v_CA21_943'], # Income categories
159159
level='CMA',
160+
labels='short',
160161
use_cache=False
161162
)
162163

pycancensus/core.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,9 @@ def _extract_vector_metadata(df, vectors, labels):
353353
if rename_dict:
354354
df = df.rename(columns=rename_dict)
355355

356-
# Store metadata as attribute (always store, but mainly useful with short labels)
357-
df.attrs["census_vectors"] = metadata_df
356+
# Store metadata as dict to avoid pandas attrs comparison bug
357+
# Convert DataFrame to list of dicts for storage
358+
df.attrs["census_vectors"] = metadata_df.to_dict(orient='records')
358359

359360
return df
360361

pycancensus/vectors.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,14 @@ def label_vectors(x):
4848
import warnings
4949

5050
if hasattr(x, "attrs") and "census_vectors" in x.attrs:
51-
return x.attrs["census_vectors"]
51+
# Convert stored dict back to DataFrame
52+
metadata = x.attrs["census_vectors"]
53+
if isinstance(metadata, list):
54+
# Stored as list of dicts
55+
return pd.DataFrame(metadata)
56+
else:
57+
# Already a DataFrame (legacy)
58+
return metadata
5259
else:
5360
warnings.warn(
5461
"Data does not have variables to labels. No Census variables selected "

0 commit comments

Comments
 (0)