Skip to content

Accept a pandas Series with labels as a location - #2327

Open
nk-batman21 wants to merge 1 commit into
python-visualization:mainfrom
nk-batman21:fix/series-location-pandas3
Open

nk-batman21 wants to merge 1 commit into
python-visualization:mainfrom
nk-batman21:fix/series-location-pandas3

Conversation

@nk-batman21

Copy link
Copy Markdown
Contributor

With pandas 3, a pandas Series that has labels is no longer accepted as a location. That is the usual shape when building a map from a DataFrame:

df = pd.DataFrame({"lat": [45.0, 46.0], "lon": [3.0, 4.0]})
folium.Map(location=df[["lat", "lon"]].mean())
for _, row in df.iterrows():
    folium.Marker(row[["lat", "lon"]])

Both lines raise TypeError: Location should support indexing, like a list or a tuple does, ..., and so do PolyLine or Polygon built from DataFrame rows. validate_location reads location[0] and location[1], which on a Series with a string index only worked through the positional fallback of Series.__getitem__. pandas 2 printed a FutureWarning for that on every call, and pandas 3 removed it, so the lookup now raises KeyError.

This converts a Series to a list before validating it, like numpy arrays and DataFrames already are. test_validate_location already accepts pd.Series([5, 3]); I added the same values with a lat/lon index.

Ran: python -m pytest --ignore=tests/selenium --ignore=tests/playwright --ignore=tests/snapshots --ignore=tests/test_repr.py on Python 3.14 with pandas 3.1.0 and on Python 3.10 with pandas 2.3.3. The new case fails before the change on pandas 3 and passes after it on both, also with -W error::FutureWarning on pandas 2.
Checked: ruff, black and mypy folium.

validate_location read location[0] and location[1]. On a Series with a
string index, such as df[["lat", "lon"]].mean() or a DataFrame row, that
relied on the positional fallback of Series.__getitem__, which pandas 2
deprecated and pandas 3 removed. Convert a Series to a list first, the
same way numpy arrays and DataFrames are handled.

This branch was successfully deployed

1 active deployment
pypi — fe21ea9c Deployed Nov 1, 2026 by nk-batman21 via packages #2034
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant