BUG: Assignment to a .loc view of a naive datetime column changes its dtype to object #49837
Labels
Dtype Conversions
Unexpected or buggy dtype conversions
Indexing
Related to indexing on series/frames, not to indexes themselves
Needs Tests
Unit test(s) needed to prevent regressions
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
When doing a partial assignment to a view created by
.loc[predicate, [column]]
in a column with dtypedatetime64[ns]
(naive datetime) the column dtype changes toobject
and the datetimes assigned are represented asfloat
s.With non-naive datetimes it works as expected, maintaining the dtype as
datetime64[ns, timezone]
.The reproducible example below prints the following:
Expected Behavior
The assignment shouldn't change the values nor the dtype of the column.
As an example, see what's shown by the reproducible example when we add
tzinfo=timezone.utc
to the values:As you can see, with a timezone the column doesn't change the dtype and the values are interpreted as
datetime
s, notfloat
s.Installed Versions
❯ python
Python 3.8.14 (default, Oct 10 2022, 16:44:50)
[GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
INSTALLED VERSIONS
commit : 91111fd
python : 3.8.14.final.0
python-bits : 64
OS : Linux
OS-release : 5.19.13-arch1-1
Version : #1 SMP PREEMPT_DYNAMIC Tue, 04 Oct 2022 14:36:58 +0000
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : es_ES.UTF-8
LOCALE : es_ES.UTF-8
pandas : 1.5.1
numpy : 1.23.5
pytz : 2022.6
dateutil : 2.8.2
setuptools : 56.0.0
pip : 22.0.4
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None
The text was updated successfully, but these errors were encountered: