A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pandas-dev/pandas/issues/45774 below:

Multi-indexed Series to Numpy fails when `na_value` supplied · Issue #45774 · pandas-dev/pandas · GitHub

Pandas version checks Reproducible Example
import numpy as np
import pandas as pd

index = pd.MultiIndex.from_tuples([(0, "a"), (0, "b")])
x = pd.Series([1, 2], index=index)

print(x.to_numpy(dtype="float")) # [1. 2.]
print(x.to_numpy(dtype="float", na_value=np.nan)) # KeyError: 1
Issue Description

When converting a multiindexed series to a numpy array, it works correctly when no na_value is specified. But when an na_value is specified, even if the series has no NA values, it errors.

Digging into this issue, when na_value is specified, a branch is taken which sets the na_value in the numpy array.
result[self.isna()] = na_value

However, the numpy result array cannot understand the multiindex of the self.isna() multi-indexed array.

The Pull Request #45775 is one possibility to address this issue.

Expected Behavior
import numpy as np
import pandas as pd

index = pd.MultiIndex.from_tuples([(0, "a"), (0, "b")])
x = pd.Series([1, 2], index=index)

print(x.to_numpy(dtype="float")) # [1. 2.]
print(x.to_numpy(dtype="float", na_value=np.nan)) # [1. 2.]
Installed Versions

The pd.show_versions() produces an error, but below are the versions of the relevant packages.

python 3.10.1 h1248fe1_2_cpython conda-forge

pandas 1.5.0.dev0+268.gbe8d1ec880 dev_0 # Installed locally
numpy 1.22.0 pypi_0 pypi


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4