BUG: wrong values produced when setting using .loc · Issue #49159 · pandas-dev/pandas (original) (raw)

Pandas version checks

Reproducible Example

import pandas as pd

df = pd.DataFrame( [ {"a": pd.Timestamp(year=2022, month=10, day=15, hour=11, minute=0),}, {"a": pd.Timestamp(year=2022, month=10, day=15, hour=12, minute=0),}, {"a": pd.Timestamp(year=2022, month=10, day=15, hour=13, minute=0),}, ] )

df["b"] = 12 # mandatory to reproduce the bug

df.loc[[1, 2], ["a"]] = df["a"] + pd.Timedelta(days=1)

print(df) print(df.dtypes)

Issue Description

the column a has now the dtype "object" instead of datetime64[ns] and has mixed types float64 and Timestamp

                     a   b
0  2022-10-15 11:00:00  12
1  1665921600000000000  12
2  1665925200000000000  12
a    object
b     int64
dtype: object

Expected Behavior

                    a  b
0 2022-10-15 11:00:00  12
1 2022-10-16 12:00:00  12
2 2022-10-16 13:00:00  12
a    datetime64[ns]
b      int64
dtype: object

Installed Versions

INSTALLED VERSIONS

commit : 87cfe4e
python : 3.8.13.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.124-linuxkit
Version : #1 SMP PREEMPT Thu Jun 30 08🔞26 UTC 2022
machine : aarch64
processor :
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.0
numpy : 1.22.2
pytz : 2021.3
dateutil : 2.8.2
setuptools : 57.5.0
pip : 22.3
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