-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC: update the Series.view docstring #20220
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -547,6 +547,66 @@ def __len__(self): | |
return len(self._data) | ||
|
||
def view(self, dtype=None): | ||
""" | ||
Create a new view of the Series. | ||
|
||
This function will return a new Series with a view of the same | ||
underlying values in memory, optionally reinterpreted with a new data | ||
type. The new data type must preserve the same size in bytes as to not | ||
cause index misalignment. | ||
|
||
Series are instantiated with `dtype=float64` by default, so unlike | ||
`numpy.array.view()` this function will not try to preserve the current | ||
Series data type. | ||
|
||
Parameters | ||
---------- | ||
dtype : data type | ||
Data type object or one of their string representations. | ||
|
||
Returns | ||
------- | ||
Series | ||
A new Series object as a view of the same data in memory. | ||
|
||
See Also | ||
-------- | ||
numpy.ndarray.view : Return a new view of the same data in memory. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say in the explanation here that this is the equivalent numpy method to take a view of an array There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But they are not exactly equivalent because numpy is a little bit more relaxed in this operation since it doesn't need to preserve the amount of data points. But you mean that perhaps I should write:
I think the current way is more formal and clear, specially because these functions don't behave exactly alike. Perhaps:
This is indeed a bit closer to the original numpy short description (New view of array with the same data), though explicitly mentioning in memory helped me making people understand why those views share data changes. Simply saying "same data" leads most people think that the data is maybe just copied. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, that is what I meant (I would say "equivalent numpy function" though). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. I'll try to be a bit more explicit on the difference between them in the Notes section. |
||
|
||
Examples | ||
-------- | ||
>>> s = pd.Series([-2, -1, 0, 1, 2], dtype='int8') | ||
>>> s | ||
0 -2 | ||
1 -1 | ||
2 0 | ||
3 1 | ||
4 2 | ||
dtype: int8 | ||
|
||
The 8 bit signed integer representation of `-1` is `0b10000001`, but | ||
the same bytes represent 255 if read as an 8 bit unsigned integer: | ||
|
||
>>> us = s.view('uint8') | ||
>>> us | ||
0 254 | ||
1 255 | ||
2 0 | ||
3 1 | ||
4 2 | ||
dtype: uint8 | ||
|
||
The views share the same underlying values: | ||
|
||
>>> us[0] = 128 | ||
>>> s | ||
0 -128 | ||
1 -1 | ||
2 0 | ||
3 1 | ||
4 2 | ||
dtype: int8 | ||
""" | ||
return self._constructor(self._values.view(dtype), | ||
index=self.index).__finalize__(self) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part I don't fully understand. Can you try to explain more what you exactly want to say, maybe with an example? (here in the comments)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's basically what I said in the first comment of this PR.
pd.Series([1,2,3], dtype='int8').view()
will raise, but the equivalent in numpy would not. Maybe I should add an example in the docstring as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes, thanks for the explanation. In principle, doing
view
without specifying a dtype is not that useful. So I would keep this explanation for the notes or so, and not include it here in the extended summary.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Notes looks like a good fit. On it.