Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment via .loc fails for subsets of columns since 0.7.0 #269

Closed
maltomato opened this issue Jan 6, 2025 · 1 comment
Closed

Assignment via .loc fails for subsets of columns since 0.7.0 #269

maltomato opened this issue Jan 6, 2025 · 1 comment

Comments

@maltomato
Copy link

Assignment via .loc fails for subsets of columns since 0.7.0

It is not possible anymore to assign values to a column using a list-like subset of the column via .loc. Using the entire column or a specific .loc for a single value works fine.

The issue was introduced with verison 0.7.0. I cannot reproduce the error with 0.6.2.

Reproduce

import pint
import pint_pandas
from pint import UnitRegistry
import pandas 

unit = UnitRegistry()
pint.set_application_registry(unit)
pint_pandas.PintType.ureg = unit

df = pd.DataFrame({
    'A': pd.Series([1, 2, 3], dtype="pint[meter]"),
    'B': pd.Series([1, 2, 3], dtype="pint[meter]"),
    'C': [0, 1, 0],
})
df

df['A'] = df['B']  # works
df.loc[0, 'A'] = df.loc[0, 'B']  # works
df.loc[[0, 1], 'A'] = df.loc[[0, 1], 'B']  # NotImplementedError
df.loc[:, 'A'] = df.loc[:, 'B']  # NotImplementedError
df.loc[df['C'] == 0, 'A'] = df.loc[df['C'] == 0, 'B']  # NotImplementedError

Error

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
Cell In[67], line 19
     17 df['A'] = df['B']  # works
     18 df.loc[0, 'A'] = df.loc[0, 'B']  # works
---> 19 df.loc[[0, 1], 'A'] = df.loc[[0, 1], 'B']  # NotImplementedError
     20 df.loc[:, 'A'] = df.loc[:, 'B']  # NotImplementedError
     21 df.loc[df['C'] == 0, 'A'] = df.loc[df['C'] == 0, 'B']  # NotImplementedError

File [~\BENTELER-Python\Lib\site-packages\pandas\core\indexing.py:911](http://localhost:8888/lab/tree/OneDrive%20-%20BENTELER%20Group/Arbeitsplatz/Code/reports/~/BENTELER-Python/Lib/site-packages/pandas/core/indexing.py#line=910), in _LocationIndexer.__setitem__(self, key, value)
    908 self._has_valid_setitem_indexer(key)
    910 iloc = self if self.name == "iloc" else self.obj.iloc
--> 911 iloc._setitem_with_indexer(indexer, value, self.name)

File [~\BENTELER-Python\Lib\site-packages\pandas\core\indexing.py:1942](http://localhost:8888/lab/tree/OneDrive%20-%20BENTELER%20Group/Arbeitsplatz/Code/reports/~/BENTELER-Python/Lib/site-packages/pandas/core/indexing.py#line=1941), in _iLocIndexer._setitem_with_indexer(self, indexer, value, name)
   1939 # align and set the values
   1940 if take_split_path:
   1941     # We have to operate column-wise
-> 1942     self._setitem_with_indexer_split_path(indexer, value, name)
   1943 else:
   1944     self._setitem_single_block(indexer, value, name)

File [~\BENTELER-Python\Lib\site-packages\pandas\core\indexing.py:1979](http://localhost:8888/lab/tree/OneDrive%20-%20BENTELER%20Group/Arbeitsplatz/Code/reports/~/BENTELER-Python/Lib/site-packages/pandas/core/indexing.py#line=1978), in _iLocIndexer._setitem_with_indexer_split_path(self, indexer, value, name)
   1976 if isinstance(value, ABCDataFrame):
   1977     self._setitem_with_indexer_frame_value(indexer, value, name)
-> 1979 elif np.ndim(value) == 2:
   1980     # TODO: avoid np.ndim call in case it isn't an ndarray, since
   1981     #  that will construct an ndarray, which will be wasteful
   1982     self._setitem_with_indexer_2d_value(indexer, value)
   1984 elif len(ilocs) == 1 and lplane_indexer == len(value) and not is_scalar(pi):
   1985     # We are setting multiple rows in a single column.

File [~\BENTELER-Python\Lib\site-packages\pint_pandas\pint_array.py:359](http://localhost:8888/lab/tree/OneDrive%20-%20BENTELER%20Group/Arbeitsplatz/Code/reports/~/BENTELER-Python/Lib/site-packages/pint_pandas/pint_array.py#line=358), in PintArray.__array_function__(self, func, types, args, kwargs)
    357 args = convert_np_inputs(args)
    358 result = func(*args, **kwargs)
--> 359 return self._convert_np_result(result)

File [~\BENTELER-Python\Lib\site-packages\pint_pandas\pint_array.py:403](http://localhost:8888/lab/tree/OneDrive%20-%20BENTELER%20Group/Arbeitsplatz/Code/reports/~/BENTELER-Python/Lib/site-packages/pint_pandas/pint_array.py#line=402), in PintArray._convert_np_result(self, result)
    400     return result
    401 else:
    402     # one return value
--> 403     return type(self)(result)

File [~\BENTELER-Python\Lib\site-packages\pint_pandas\pint_array.py:327](http://localhost:8888/lab/tree/OneDrive%20-%20BENTELER%20Group/Arbeitsplatz/Code/reports/~/BENTELER-Python/Lib/site-packages/pint_pandas/pint_array.py#line=326), in PintArray.__init__(self, values, dtype, copy)
    324         dtype = values._dtype
    326 if dtype is None:
--> 327     raise NotImplementedError
    329 if not isinstance(dtype, PintType):
    330     dtype = PintType(dtype)

NotImplementedError:

Context

  • Windwows
  • Python 3.12.5
  • pint-pandas 0.7.0
  • pandas 2.2.3
@andrewgsavage
Copy link
Collaborator

fixed with #270

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

No branches or pull requests

2 participants