question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: Unexpected behavior using loc to assign value in a for loop

See original GitHub issue

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

# multiple assignement using index loc like code below (column name may vary)
for ... in ...:
   [...]
   indx_measure_started = df_meas[df_meas.key == something].index
   df_meas.loc[indx_measure_started, "Vitesse Traverse"] = numpy.hstack(([0], numpy.abs(delta_position / delta_t)))

Issue Description

I don’t know why but there is a strange behavior after multiple use of loc for assignement. I’m sorry to not be able to give you a more precise way to reproduce… I tried to debug each line and don’t understand what is happening (may be something in the indexing.py file)

Using a setting value method like

df_meas.loc[indx_measure_started, "Vitesse Traverse"] = numpy.hstack(([0], numpy.abs(delta_position / delta_t)))

I can no more access the value using loc. Note that in the image below if I use loc + iloc I find the computed values… image

Is it a kind of memory leaks ?

Expected Behavior

I expect to have the values readable…

TIPS :

  • Behaviour as expected with 1.2.5, 1.3.5
  • KO for 1.4.0

Installed Versions

Replace this line with the output of 1.4.3

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
phoflcommented, Jul 29, 2022

Will close then, please ping to reopen if you have a reproducible example

0reactions
jrebackcommented, Jul 30, 2022

@jultou-raa if you have a reproducible example you can ping to reopen as was stated above

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unexpected behavior in Pandas indexing with .loc
I have a pandas data frame, called data, that has datetime indices. When I attempt to index the data by multiple datetimes, an...
Read more >
Python – Pandas Dataframe using .loc for assignment gives ...
I am doing some calculations in pandas and the .loc method is having unexpected results. not sure if it is me misusing the...
Read more >
Unexpected behavior (suspected bug) when using Mata views with ...
I encountered a very strange behavior when I use Mata views on temporary variables created by fvrevar. I tried to keep the following...
Read more >
SettingwithCopyWarning: How to Fix This Warning in Pandas
It's no surprise that many struggle with this; there are so many ways to ... Setting the new value data.loc[data.bidder == 'parakeet2004', ...
Read more >
A Python Beginner's Look at .loc. As a ... - Towards Data Science
As a Python beginner, using .loc to retrieve and update values in a pandas ... Passing just a column label or a blank...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found