-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: dt.strftime causes TypeError when there is only one row in DataFrame #15494
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
Labels
Bug
Dtype Conversions
Unexpected or buggy dtype conversions
Indexing
Related to indexing on series/frames, not to indexes themselves
Milestone
Comments
More comprehensive reproducible example:
And indeed works when having more than 1 row. |
yeah |
I am relatively new to pandas... Is the workaround suggested in the original script (df['date'] = df['date'].dt...) completely equivalent ?
Thank You
…Sent from my iPhone
On Feb 24, 2017, at 07:11, Jeff Reback ***@***.***> wrote:
yeah .loc does inference on the resulting column to potentially change the dtype, looks like an uncaught path.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Yes, |
This appears to be fixed on 0.22.0 (so really probably on 0.21.x too): In [1]: import pandas as pd; import datetime as dt
In [2]: pd.__version__
Out[2]: '0.22.0'
In [3]: df1 = pd.DataFrame({'date': pd.Series([dt.datetime.today()])})
In [4]: df1.loc[:,'date'] = 'string'
In [5]: df1
Out[5]:
date
0 string Could still use a test to ensure there's not a regression. |
3 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Bug
Dtype Conversions
Unexpected or buggy dtype conversions
Indexing
Related to indexing on series/frames, not to indexes themselves
Code below outlines the issue, if I have a DataFrame with a date column and want to transfer the datetime to string using df.loc[:,'date'] = df['date'].dt.strftime('%Y-%m-%d'), it works fine as long as there is more than 1 row in the Data Frame. If there is only one row then you get a Type Error (see below for full error). If, on the other hand, I use df['date']=df['date'].dt.strftime('%Y-%m-%d') then it works fine for one or more rows. This was observed on both 0.17.1 and 0.19.2.
----- Error Generated -----
Traceback (most recent call last):
File "/usr/local/lib/python3.4/site-packages/pandas/core/internals.py", line 2265, in _try_coerce_args
other = other.astype('i8', copy=False).view('i8')
ValueError: invalid literal for int() with base 10: '2017-02-23'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./testpd.py", line 35, in
do_stuff(d1)
File "./testpd.py", line 17, in do_stuff
df.loc[:,'date'] = df['date'].dt.strftime('%Y-%m-%d')
File "/usr/local/lib/python3.4/site-packages/pandas/core/indexing.py", line 141, in setitem
self._setitem_with_indexer(indexer, value)
File "/usr/local/lib/python3.4/site-packages/pandas/core/indexing.py", line 533, in _setitem_with_indexer
setter(labels[0], value)
File "/usr/local/lib/python3.4/site-packages/pandas/core/indexing.py", line 473, in setter
s._data = s._data.setitem(indexer=pi, value=v)
File "/usr/local/lib/python3.4/site-packages/pandas/core/internals.py", line 3168, in setitem
return self.apply('setitem', **kwargs)
File "/usr/local/lib/python3.4/site-packages/pandas/core/internals.py", line 3056, in apply
applied = getattr(b, f)(**kwargs)
File "/usr/local/lib/python3.4/site-packages/pandas/core/internals.py", line 668, in setitem
values, _, value, _ = self._try_coerce_args(self.values, value)
File "/usr/local/lib/python3.4/site-packages/pandas/core/internals.py", line 2270, in _try_coerce_args
raise TypeError
TypeError
----- Code to Reproduce Issue -----
The text was updated successfully, but these errors were encountered: