-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Dataframe column dtype changed from int8 to int64 when setting complete column #11638
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
Comments
hmm, I recall seeing almost the same issue, but can't locate ATM. yep, looks buggy. pull-requests to fix are welcome. |
@jreback In comman.py # provide implicity upcast on scalars
elif is_integer(val):
dtype = np.int64
elif is_float(val):
dtype = np.float64 if there is no specific requirement for upcasting, then I can do a PR . |
this is actually tricky. do alternatively you can do
etc (this might be better) |
We are calling _infer_dtype_from_scalar(val) which is already doing this job. How does it solve the problem ? Am I missing something ? |
right......so must be someplace else then (because |
As pointed earlier , how about modifying _infer_dtype_from_scalar(val) and use np.issubdtype inside is_integer(val) and is_float(val) function to provide support for all kind of int and float types ? I am assuming that this will generate the same error for float types as well. |
how would that help? |
If we modify integer and float conditions in _infer_dtype_from_scalar(val) like this elif is_integer(val):
if isinstance(val, int):
dtype = np.int64
else:
dtype = type(val)
elif is_float(val):
if isinstance(val, float):
dtype = np.float64
else:
dtype = type(val) It will resolve the discrepancy without breaking anything. Please suggest . |
ahh, the problem is this:
you can try that change that you are suggesting above and see what breaks (and of course add a test for this behavior). It looks like it should work. |
Added test case TestInferDtype
closed by #11644 |
Wow, thank you for the quick fix. I was going to try but got lost in the pandas internals. Maybe next time 😄 |
COMPAT: compat of scalars on all platforms, xref #11638
thank @varun-kr ! |
Version 0.17.1 * tag 'v0.17.1': (168 commits) add nbviewer link Revert "DOC: fix sponsor notice" DOC: a few touchups DOC: fix sponsor notice DOC: warnings and remove HTML COMPAT: compat of scalars on all platforms, xref pandas-dev#11638 DOC: fix build errors/warnings DOC: whatsnew edits DOC: fix link syntax DOC: update release.rst / whatsnew edits BUG: fix col iteration in DataFrame.round, pandas-dev#11611 DOC: Clarify foramtting BUG: pandas-dev#11638 return correct dtype for int and float BUG: pandas-dev#11637 fix to_csv incorrect output. DOC: sponsor notice BUG: indexing with a range , pandas-dev#11652 Fix link to numexpr ENH: fixup tilde expansion, xref pandas-dev#11438 ENH: tilde expansion for write output formatting functions, pandas-dev#11438 DOC: fix up doc-string creations in generic.py ...
The following example should explain:
So it is cast to the correct dtype if a slice of the column is changed but setting the whole column changes the dtype even when explicitly set to
np.int8
The text was updated successfully, but these errors were encountered: