Skip to content

BUG: Data inserted with from order when using .loc #48829

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
2 of 3 tasks
kenha37 opened this issue Sep 28, 2022 · 1 comment
Closed
2 of 3 tasks

BUG: Data inserted with from order when using .loc #48829

kenha37 opened this issue Sep 28, 2022 · 1 comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@kenha37
Copy link

kenha37 commented Sep 28, 2022

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

import pandas as pd
import datetime as dt

l = 100
vals = [i for i in range(l)]

df = pd.DataFrame({'Time': [dt.datetime.now() - dt.timedelta(hours=i) for i in range(l)], 'A': vals, 'B':  vals, 'C': vals})
data = df.copy()
df.loc[[True for _ in range(l)], ['A', 'B']] = df.loc[[True for _ in range(l)], ['A', 'B']]
print(data.equals(df))

Issue Description

When inserting an entire column using the loc function and a boolean index we observe the the values are not inserted in the correct order.
This only happens when the entire column is changed, i.e. the entire boolean array is True, and if there is a datetime column in the dataframe.

Expected Behavior

This behavior was tested in pandas 1.4.4 and the problem wasn't observed here.
The two below examples behave as expected and retains the data order.

# Works with one not True
df = pd.DataFrame({'Time': [dt.datetime.now() - dt.timedelta(hours=i) for i in range(l)], 'A': vals, 'B':  vals, 'C': vals})
data = df.copy()
df.loc[[True for _ in range(l-1)] + [False], ['A', 'B']] = df.loc[[True for _ in range(l-1)] + [False], ['A', 'B']]
print(data.equals(df))

# Works with only float/int data
df = pd.DataFrame({'A': vals, 'B':  vals, 'C': vals})
data = df.copy()
df.loc[[True for _ in range(l)], ['A', 'B']] = df.loc[[True for _ in range(l)], ['A', 'B']]
print(data.equals(df))

Installed Versions

INSTALLED VERSIONS

commit : 87cfe4e
python : 3.9.6.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22000
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : Danish_Denmark.1252

pandas : 1.5.0
numpy : 1.23.3
pytz : 2022.2.1
dateutil : 2.8.2
setuptools : 62.1.0
pip : 22.1.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
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

@kenha37 kenha37 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 28, 2022
@kenha37 kenha37 changed the title BUG: BUG: Data inserted with from order when using .loc Sep 28, 2022
@phofl
Copy link
Member

phofl commented Sep 28, 2022

Duplicate of #48701

@phofl phofl marked this as a duplicate of #48701 Sep 28, 2022
@phofl phofl closed this as completed Sep 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

2 participants