Skip to content

setitem type coercion fails when setting a datetime column in a single row dataframe #14179

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
tambu-j opened this issue Sep 7, 2016 · 3 comments
Labels
Bug Datetime Datetime data dtype Duplicate Report Duplicate issue or pull request Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@tambu-j
Copy link

tambu-j commented Sep 7, 2016

xref #6942
xref #12499

import datetime
import pandas as pd
df1 = pd.DataFrame({'a': [0,1], 'b': [datetime.datetime.now(), datetime.datetime.now()]})
# next line works as expected
df1.loc[:,'b'] = [datetime.datetime.now(), datetime.datetime.now()]
df1 = pd.DataFrame({'a': [0], 'b': [datetime.datetime.now()]})
# next line blows up 
df1.loc[:,'b'] = [datetime.datetime.now()]

Exception:
/Users//anaconda/lib/python2.7/site-packages/pandas/core/internals.py in _try_coerce_args(self, values, other)
2103 other_mask = isnull(other)
2104
-> 2105 other = other.astype('i8', copy=False).view('i8')
2106 except ValueError:
2107

TypeError: long() argument must be a string or a number, not 'datetime.datetime'

Incidentally, df1['b'] = [datetime.datetime.now()] will work, but i'm avoiding that as the performance is so poor when it's a copy (in my particular case the copy is dead and gc.collect(2) gets called for each column set operation, blowing out the runtime to 3x what it was pre-copy checking - should I open a separate issue for that?).

pd.show_versions():
commit: None
python: 2.7.12.final.0
python-bits: 64
OS: Darwin
OS-release: 12.6.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8

pandas: 0.18.1
nose: 1.3.7
pip: 8.1.1
setuptools: 20.3
Cython: 0.23.4
numpy: 1.10.4
scipy: 0.17.0
statsmodels: 0.6.1
xarray: None
IPython: 5.1.0
sphinx: 1.3.5
patsy: 0.4.0
dateutil: 2.5.1
pytz: 2016.2
blosc: None
bottleneck: 1.0.0
tables: 3.2.2
numexpr: 2.5
matplotlib: 1.5.1
openpyxl: 2.3.2
xlrd: 0.9.4
xlwt: 1.0.0
xlsxwriter: 0.8.4
lxml: 3.6.0
bs4: 4.4.1
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.12
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.39.0
pandas_datareader: None

@jreback jreback added Bug Datetime Datetime data dtype Indexing Related to indexing on series/frames, not to indexes themselves Difficulty Intermediate labels Sep 8, 2016
@jreback jreback added this to the Next Major Release milestone Sep 8, 2016
@jreback
Copy link
Contributor

jreback commented Sep 8, 2016

this is the same cause as #6942, #12499

@jreback
Copy link
Contributor

jreback commented Sep 8, 2016

@tambu-j

Incidentally, df1['b'] = [datetime.datetime.now()] will work, but i'm avoiding that as the performance is so poor when it's a copy (in my particular case the copy is dead and gc.collect(2) gets called for each column set operation, blowing out the runtime to 3x what it was pre-copy checking - should I open a separate issue for that?).

you are modifying a filtered frame. you should simply copy before you modify as the warning indicates.

@jreback
Copy link
Contributor

jreback commented Oct 30, 2017

dupe of #6942

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Duplicate Report Duplicate issue or pull request Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

No branches or pull requests

2 participants