Skip to content

Commit aa3c228

Browse files
committed
Cleaned up work comments.
1 parent 91442a8 commit aa3c228

File tree

3 files changed

+15
-122
lines changed

3 files changed

+15
-122
lines changed

pandas/core/arrays/_mixins.py

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -155,83 +155,6 @@ def view(self, dtype: Dtype | None = None) -> ArrayLike:
155155
# "ExtensionDtype | dtype[Any]"; expected "dtype[Any] | _HasDType[dtype[Any]]"
156156
return arr.view(dtype=dtype) # type: ignore[arg-type]
157157

158-
159-
# Notes on take method fix
160-
# Please remove these once this fix is ready to submit.
161-
# =======================================================================
162-
163-
# One of the base classes to this class: ExtensionArray, provides
164-
# the dtype property, but abstractly, so it leaves the implementation
165-
# of dtype storage up to its derived classes. Some of these derived
166-
# classes don't provide a setter method for their dtype property, so
167-
# I can't set the dtype here and expect it to work for all classes that
168-
# inherit this take method.
169-
170-
# How can I produce an extension array of the same type as self,
171-
# having a floating-point dtype if self has an integer dtype or otherwise
172-
# the same dtype as self?
173-
174-
# Constructing a new object of the same type as self doesn't always
175-
# work since the constructors of some derived classes of this class
176-
# don't accept a dtype parameter, which I need to pass to set the
177-
# result's dtype to a floating-point type.
178-
179-
# All tests pass when I create a new extension array object with the
180-
# appropriate dtype (in the integer-dtype source case), however MyPy
181-
# complains about the missing dtype argument in the call to type(self)
182-
# below. By creating a new array object, this call produces an array
183-
# with a floating point dtype, even when the source dtype is integral.
184-
# I think this happens because the new array is created with the newly
185-
# produced data from the underlying take method, which has the
186-
# appropriate underlying dtype.
187-
188-
# Essentially, these extension arrays are wrappers around Numpy arrays
189-
# which have their own dtype and store the data. Thus, the new
190-
# extension array inherits the dtype from the Numpy array used
191-
# to create it.
192-
193-
# Unfortunately, some of the derived constructors of this class have a
194-
# positional dtype argument, while some do not. If I call a constructor
195-
# without specifying this argument, mypy will complain about the
196-
# missing argument in the case of constructors that require it, but
197-
# if I call the constructor with the dtype argument, the constructors
198-
# that don't have it will fail at runtime since they don't recognize
199-
# it.
200-
201-
# How can I get around this issue?
202-
# Ideas:
203-
# Modify the extension array type to allow modification of its dtype
204-
# after construction.
205-
206-
# Add a conditional branch to this method to call derived constructors
207-
# with or without the dtype argument, depending on their class.
208-
# This approach has the disadvantage of hardcoding information about
209-
# derived classes in this base class, which means that if someone
210-
# changes a constructor of a derived class to remove the dtype argument,
211-
# this method will break.
212-
213-
# Classes derived from this class include:
214-
215-
# Categorical
216-
# DatetimeLikeArrayMixin
217-
# DatelikeOps
218-
# PeriodArray
219-
# DatetimeArray
220-
# TimelikeOps
221-
# TimedeltaArray
222-
# NumpyExtensionArray
223-
# StringArray
224-
225-
# The types of extension arrays (within Pandas) derived from this class are:
226-
# Class name Constructor takes dtype argument Dtype argument required
227-
# Categorical yes no
228-
# PeriodArray yes no
229-
# DatetimeArray
230-
# TimedeltaArray
231-
# StringArray yes no
232-
# NumpyExtensionArray no no
233-
234-
235158
def take(
236159
self,
237160
indices: TakeIndexer,

pandas/core/arrays/numpy_.py

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,9 @@ def take(
363363
Take entries from this array at each index in a list of indices,
364364
producing an array containing only those entries.
365365
"""
366+
result = super().take(
367+
indices, allow_fill=allow_fill, fill_value=fill_value, axis=axis
368+
)
366369
# See GH#62448.
367370
if self.dtype.numpy_dtype in [
368371
np.uint8,
@@ -372,51 +375,11 @@ def take(
372375
np.int8,
373376
np.int16,
374377
np.int32,
375-
np.int64
378+
np.int64,
376379
]:
377-
# In this case, the resulting extension array should have a floating-point
378-
# dtype to match the result of the underlying take method when
379-
# NaN values need to be incorporated into it.
380-
# This occurs when allow_fill is True and fill_value is None.
381-
# (fill_value may be an arbitrary Python object, in which case
382-
# the result will be an array of objects.)
383-
384-
# Call the take method of NDArrayBackedExtensionArray
385-
386-
# TODO: How is the dtype of a newly constructed NumpyExtensionArray set?
387-
# It's set to match the dtype of its underlying array.
388-
389-
result = super().take(
390-
indices,
391-
allow_fill=allow_fill,
392-
fill_value=fill_value,
393-
axis=axis
394-
)
395380
return type(self)(result, copy=False)
396-
397-
# In this case, the resulting extension array will have a dtype
398-
# that matches that of the underlying Numpy array and we can link
399-
# to the underlying array without manipulating the extension's
400-
# dtype.
401-
402-
return super().take(
403-
indices,
404-
allow_fill=allow_fill,
405-
fill_value=fill_value,
406-
axis=axis
407-
)
408-
# result array dtype = self dtype
409-
410-
# Implementation steps:
411-
# Determine requirements for this method, including:
412-
# Argument types [done]
413-
# Return type [done]
414-
# Return dtype [done]
415-
# Write tests to check whether this method satisfies these requirements. [done]
416-
# Figure out what base class method to call to implement the take functionality. [done]
417-
# Implement the call. [done]
418-
# Check whether this method satisfies its requirements by running the tests.
419-
381+
382+
return result
420383

421384
# ------------------------------------------------------------------------
422385
# Reductions

pandas/tests/arrays/numpy_/test_numpy.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,10 @@ def test_factorize_unsigned():
326326

327327

328328
# TODO: Add the smaller width dtypes to the parameter sets of these tests.
329-
@pytest.mark.parametrize("dtype", [np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int16, np.int32, np.int64])
329+
@pytest.mark.parametrize(
330+
"dtype",
331+
[np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int16, np.int32, np.int64],
332+
)
330333
def test_take_assigns_floating_point_dtype(dtype):
331334
# GH#62448.
332335
array = NumpyExtensionArray(np.array([1, 2, 3], dtype=dtype))
@@ -339,7 +342,11 @@ def test_take_assigns_floating_point_dtype(dtype):
339342

340343
assert result.dtype.numpy_dtype == np.float64
341344

342-
@pytest.mark.parametrize("dtype", [np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int16, np.int32, np.int64])
345+
346+
@pytest.mark.parametrize(
347+
"dtype",
348+
[np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int16, np.int32, np.int64],
349+
)
343350
def test_take_assigns_integer_dtype_when_fill_disallowed(dtype):
344351
# GH#62448.
345352
array = NumpyExtensionArray(np.array([1, 2, 3], dtype=dtype))

0 commit comments

Comments
 (0)