Code Monkey home page Code Monkey logo

Comments (7)

Animesh-Shukla avatar Animesh-Shukla commented on August 30, 2024

can I take this issue ?

from pandas.

Animesh-Shukla avatar Animesh-Shukla commented on August 30, 2024

take

from pandas.

Siddharth-Latthe-07 avatar Siddharth-Latthe-07 commented on August 30, 2024

@sunghjung3 I think it seems to be a regression in recent versions of pandas. It appears that a 1-dimensional numpy array with a single entry is being automatically converted to a 0-dimensional array when stored in a DataFrame.
Temporary Workaround:

  1. Until the issue is resolved in future pandas releases, you can explicitly check and reshape the arrays as needed.
import pandas as pd
import numpy as np

df = pd.DataFrame(columns=['x'])
arr0 = np.array([1., 2.])
df.loc[0, "x"] = arr0
print(df.loc[0, "x"].shape)  # prints (2,)

arr1 = np.array([1.])
df.loc[1, "x"] = arr1 if arr1.shape != () else arr1.reshape(1,)
print(df.loc[1, "x"].shape)  # prints (1,)

Hope this helps,
Thanks

from pandas.

sunghjung3 avatar sunghjung3 commented on August 30, 2024

@Siddharth-Latthe-07 I've already tried this, and it doesn't work. It still prints (). Please try it yourself.

from pandas.

Siddharth-Latthe-07 avatar Siddharth-Latthe-07 commented on August 30, 2024

oh, I see then can we create a custom function to ensure that the shape of the numpy array is preserved when you store it in the DataFrame. This function will explicitly check the shape of the array and reshape it if necessary before storing it in the DataFrame.
Thoughts ? @sunghjung3

from pandas.

Siddharth-Latthe-07 avatar Siddharth-Latthe-07 commented on August 30, 2024

something like this:-

def safe_loc_assign(df, index, column, value):
    if isinstance(value, np.ndarray) and value.shape == ():
        value = value.reshape(1,)
    df.at[index, column] = value

from pandas.

sunghjung3 avatar sunghjung3 commented on August 30, 2024

@Siddharth-Latthe-07 I don't get the logic of your function.

I'm assuming that we would call this function like safe_loc_assign(df, 1, "x", arr1). And inside this function, it checks for value.shape == (). But the shape of the value argument (which is arr1) is not (), so this if statement isn't even utilized.

And the result is still the same; it prints ().

Also, even if I change this if-statement to value.shape == (1,), it still doesn't work.

from pandas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.