-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Align Series.str.zfill() with str.zfill() #21874
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2557,20 +2557,26 @@ def zfill(self, width): | |
|
||
Note that ``10`` and ``NaN`` are not strings, therefore they are | ||
converted to ``NaN``. The minus sign in ``'-1'`` is treated as a | ||
regular character and the zero is added to the left of it | ||
special character and the zero is added to the right of it | ||
(:meth:`str.zfill` would have moved it to the left). ``1000`` | ||
remains unchanged as it is longer than `width`. | ||
|
||
>>> s.str.zfill(3) | ||
0 0-1 | ||
0 -01 | ||
1 001 | ||
2 1000 | ||
3 NaN | ||
4 NaN | ||
dtype: object | ||
""" | ||
result = str_pad(self._data, width, side='left', fillchar='0') | ||
return self._wrap_result(result) | ||
|
||
if not is_integer(width): | ||
msg = 'width must be of integer type, not {0}' | ||
raise TypeError(msg.format(type(width).__name__)) | ||
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. Add test for this validation. |
||
|
||
f = lambda x: x.zfill(width) | ||
|
||
return self._wrap_result(_na_map(f, self._data)) | ||
|
||
@copy(str_slice) | ||
def slice(self, start=None, stop=None, step=None): | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2220,27 +2220,31 @@ def test_center_ljust_rjust_fillchar(self): | |
result = values.str.rjust(5, fillchar=1) | ||
|
||
def test_zfill(self): | ||
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. the issue mentions also affecting |
||
values = Series(['1', '22', 'aaa', '333', '45678']) | ||
values = Series(['1', '+1', '-1', '22', 'aaa', '333', '45678']) | ||
|
||
result = values.str.zfill(5) | ||
expected = Series(['00001', '00022', '00aaa', '00333', '45678']) | ||
expected = Series(['00001', '+0001', '-0001', '00022', '00aaa', | ||
'00333', '45678']) | ||
tm.assert_series_equal(result, expected) | ||
expected = np.array([v.zfill(5) for v in values.values], | ||
dtype=np.object_) | ||
tm.assert_numpy_array_equal(result.values, expected) | ||
|
||
result = values.str.zfill(3) | ||
expected = Series(['001', '022', 'aaa', '333', '45678']) | ||
expected = Series(['001', '+01', '-01', '022', 'aaa', '333', '45678']) | ||
tm.assert_series_equal(result, expected) | ||
expected = np.array([v.zfill(3) for v in values.values], | ||
dtype=np.object_) | ||
tm.assert_numpy_array_equal(result.values, expected) | ||
|
||
values = Series(['1', np.nan, 'aaa', np.nan, '45678']) | ||
values = Series(['1', np.nan, 'aaa', np.nan, '45678', 10]) | ||
result = values.str.zfill(5) | ||
expected = Series(['00001', np.nan, '00aaa', np.nan, '45678']) | ||
expected = Series(['00001', np.nan, '00aaa', np.nan, '45678', np.nan]) | ||
tm.assert_series_equal(result, expected) | ||
|
||
with tm.assert_raises_regex(TypeError, "width must be of integer"): | ||
values.str.zfill('5') | ||
|
||
def test_split(self): | ||
values = Series(['a_b_c', 'c_d_e', NA, 'f_g_h']) | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
see comments, make this a sub section in api breaking (yes its a bug fix, but an unexpected one)
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.
Also, parentheses not needed at the end of
zfill
in the :func: ref.