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

Add casting keyword to numeric array types. #547

Merged
merged 11 commits into from
Aug 3, 2020
Merged

Conversation

k2bd
Copy link
Contributor

@k2bd k2bd commented Nov 7, 2019

This adds a casting keyword to numpy array types. This is passed to astype if the specified and incoming value's dtype do not match. This allows the user to, for example, only allow values to be set that can be safely cast (casting="safe"). Currently "unsafe" is used as it's astype's default.

The "if self.coerce:" block is removed from within AbstractArray as I believe it was nonfunctional. Within the containing "if ... value.dtype != self.dtype:" block, it only seems to control whether or not the stored array must be a copy of the incoming value in the case that the incoming value is already a numpy array and the dtypes matched (asarray vs astype). However, that block is only entered if the dtypes don't match, so this case would not come up.

@codecov-io
Copy link

codecov-io commented Nov 7, 2019

Codecov Report

Merging #547 into master will increase coverage by 0.16%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #547      +/-   ##
==========================================
+ Coverage   72.99%   73.16%   +0.16%     
==========================================
  Files          51       51              
  Lines        6474     6473       -1     
  Branches     1302     1301       -1     
==========================================
+ Hits         4726     4736      +10     
+ Misses       1349     1342       -7     
+ Partials      399      395       -4
Impacted Files Coverage Δ
traits/trait_numeric.py 58.66% <100%> (+7.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8d539c9...dc1d59b. Read the comment docs.

@mdickinson mdickinson self-requested a review January 3, 2020 09:57
@mdickinson mdickinson self-assigned this Jan 3, 2020
@mdickinson mdickinson removed their assignment Jun 29, 2020
@mdickinson mdickinson removed their request for review June 29, 2020 11:36
@coreysharris
Copy link

@mdickinson This is exactly what I was asking for in #1244 😄

Copy link
Contributor

@kitchoi kitchoi left a comment

Choose a reason for hiding this comment

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

LGTM.
I think @mdickinson wanted to look into this too.

This makes the coerce attribute entirely an no-opt (but it will still be defined, and will need to be for backward compatibility). It could be a separate PR to update the documentation and things.

@k2bd
Copy link
Contributor Author

k2bd commented Jul 28, 2020

Thank you! Fixed the merge conflict

Copy link
Member

@mdickinson mdickinson left a comment

Choose a reason for hiding this comment

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

LGTM: one request for a docstring update, and one question/suggestion about making the casting parameter keyword-only.

@@ -351,13 +365,29 @@ class CArray(AbstractArray):
second dimension must be at least 2.)
value : numpy array
A default value for the array.
casting : str
Casting rule for ``numpy.ndarray.astype``. Values that cannot be
Copy link
Member

Choose a reason for hiding this comment

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

Can we reword this docstring a bit to say what the effect of this keyword is, so that the reader doesn't have to go digging into the NumPy documentation to figure it out?

It may also be worth clarifying that this parameter is not used if dtype is not given.

Copy link
Member

Choose a reason for hiding this comment

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

I was thinking of something along the lines of:

Rule used to convert array values to the target dtype. The default is "unsafe", which
allows arbitrary data conversions. "safe" permits only conversions that don't change
the value (so for example conversion from float64 to float32 would not be permitted).
The meaning and behaviour are exactly as in numpy.ndarray.astype. see the NumPy
documentation for other options for this parameter.

Copy link
Member

Choose a reason for hiding this comment

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

For bonus points, we could even make this a proper Sphinx link and add NumPy to the intersphinx configuration.

shape=None,
value=None,
typecode=None,
casting="unsafe",
Copy link
Member

Choose a reason for hiding this comment

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

Consider making this parameter keyword-only (here and elsewhere)? I don't think there's any good case for passing this by position.

@k2bd k2bd requested a review from mdickinson August 3, 2020 16:17
@mdickinson
Copy link
Member

LGTM!

@mdickinson mdickinson merged commit 91cd08c into master Aug 3, 2020
@mdickinson mdickinson deleted the array_casting_kword branch August 3, 2020 16:22
@mdickinson mdickinson mentioned this pull request Aug 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants