Skip to content

REF: Simplify Datetimelike constructor dispatching #23140

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

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f13cc58
Avoid non-public constructors
jbrockmendel Oct 13, 2018
4188ec7
simplify and de-duplicate _generate_range
jbrockmendel Oct 13, 2018
7804f1b
Check for invalid axis kwarg
jbrockmendel Oct 13, 2018
a4775f4
Move some EA properties up to mixins
jbrockmendel Oct 13, 2018
8ee34fa
implement basic TimedeltaArray tests
jbrockmendel Oct 13, 2018
78943c1
clean up PeriodArray constructor, with tests
jbrockmendel Oct 13, 2018
aa71383
make PeriodArray.__new__ more grown-up
jbrockmendel Oct 13, 2018
eae8389
Remove unused kwargs from TimedeltaArray.__new__
jbrockmendel Oct 13, 2018
e871733
revert change that broke tests
jbrockmendel Oct 13, 2018
7840f91
Fixup whitespace
jbrockmendel Oct 14, 2018
ec50b0b
helper function for axis validation
jbrockmendel Oct 14, 2018
eb7a6b6
suggested clarifications
jbrockmendel Oct 14, 2018
32c6391
Merge branch 'dlike8' of https://github.com/jbrockmendel/pandas into …
jbrockmendel Oct 14, 2018
c903917
Merge branch 'master' of https://github.com/pandas-dev/pandas into dl…
jbrockmendel Oct 14, 2018
b97ec96
move axis validation to nv
jbrockmendel Oct 14, 2018
11db555
Merge branch 'master' of https://github.com/pandas-dev/pandas into dl…
jbrockmendel Oct 14, 2018
147de57
revert some removals
jbrockmendel Oct 14, 2018
7c4d281
Merge branch 'master' of https://github.com/pandas-dev/pandas into dl…
jbrockmendel Oct 15, 2018
b90f421
catch too-negative values
jbrockmendel Oct 15, 2018
dc4f474
Roll validate_minmax_axis into existing validate functions
jbrockmendel Oct 15, 2018
46d5e64
fixup typo
jbrockmendel Oct 15, 2018
b5827c7
Merge branch 'master' of https://github.com/pandas-dev/pandas into dl…
jbrockmendel Oct 17, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
helper function for axis validation
  • Loading branch information
jbrockmendel committed Oct 14, 2018
commit ec50b0bd7b98ab9ddf40a8ed121f4629be3d033f
39 changes: 23 additions & 16 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,7 @@ def min(self, axis=None, *args, **kwargs):
--------
numpy.ndarray.min
"""
if axis is not None and axis >= self.ndim:
raise ValueError("`axis` must be fewer than the number of "
"dimensions ({ndim})".format(ndim=self.ndim))

_validate_minmax_axis(axis)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not what i mean
add this specifically to no.validate_* there are mechanisms for this already

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, done.

nv.validate_min(args, kwargs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there reason not to add the axis validation to the existing validate_min ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly I don't want another function, rather you can simply check this in side the function which is already there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I'm not wild about the fact that the nv.validate_(min|max|argmin|argmax) functions now implicitly assume they are only being called on 1-dim objects, but at least the assumption is correct for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, yeah, that makes sense.
And adding them in a single validation is actually also mixing two kinds of validation: validation of arguments that are purely for numpy compat (things like out), opposed to validation of valid arguments for pandas (axis in the Series and Index methods is also there for consistency with DataFrame, than for compat with numpy)


try:
Expand Down Expand Up @@ -462,10 +459,7 @@ def argmin(self, axis=None, *args, **kwargs):
--------
numpy.ndarray.argmin
"""
if axis is not None and axis >= self.ndim:
raise ValueError("`axis` must be fewer than the number of "
"dimensions ({ndim})".format(ndim=self.ndim))

_validate_minmax_axis(axis)
nv.validate_argmin(args, kwargs)

i8 = self.asi8
Expand All @@ -486,10 +480,7 @@ def max(self, axis=None, *args, **kwargs):
--------
numpy.ndarray.max
"""
if axis is not None and axis >= self.ndim:
raise ValueError("`axis` must be fewer than the number of "
"dimensions ({ndim})".format(ndim=self.ndim))

_validate_minmax_axis(axis)
nv.validate_max(args, kwargs)

try:
Expand Down Expand Up @@ -518,10 +509,7 @@ def argmax(self, axis=None, *args, **kwargs):
--------
numpy.ndarray.argmax
"""
if axis is not None and axis >= self.ndim:
raise ValueError("`axis` must be fewer than the number of "
"dimensions ({ndim})".format(ndim=self.ndim))

_validate_minmax_axis(axis)
nv.validate_argmax(args, kwargs)

i8 = self.asi8
Expand Down Expand Up @@ -722,6 +710,25 @@ def _time_shift(self, periods, freq=None):
return result


def _validate_minmax_axis(axis):
"""
Ensure that the axis argument passed to min, max, argmin, or argmax is
zero or None, as otherwise it will be incorrectly ignored.

Parameters
----------
axis : int or None

Raises
------
ValueError
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see my comment above

ndim = 1 # hard-coded for Index
if axis is not None and axis >= ndim:
raise ValueError("`axis` must be fewer than the number of "
"dimensions ({ndim})".format(ndim=ndim))


def _ensure_datetimelike_to_i8(other, to_utc=False):
"""
helper for coercing an input scalar or array to i8
Expand Down
10 changes: 5 additions & 5 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,11 @@ def __new__(cls, data=None,

if data is None:
# TODO: Remove this block and associated kwargs; GH#20535
out = cls._generate_range(start, end, periods,
freq=freq, tz=tz, normalize=normalize,
closed=closed, ambiguous=ambiguous)
out.name = name
return out
result = cls._generate_range(start, end, periods,
freq=freq, tz=tz, normalize=normalize,
closed=closed, ambiguous=ambiguous)
result.name = name
return result

if not isinstance(data, (np.ndarray, Index, ABCSeries,
DatetimeArrayMixin)):
Expand Down