Let's say I have this DataFrame:
df=pd.DataFrame([[2, 3], [4, 5], [6, 7]], index=pd.MultiIndex.from_tuples([
(pd.Timestamp('2019-07-01 23:00:00'), pd.Timestamp('2019-07-01 23:00:00'), 0),
(pd.Timestamp('2019-07-02 00:00:00'), pd.Timestamp('2019-07-02 00:00:00'), 0),
(pd.Timestamp('2019-07-02 00:00:00'), pd.Timestamp('2019-07-02 00:00:00'), 0)],
names=['dt_calc', 'dt_fore', 'positional_index']), columns=['temp', 'temp_2'])
idx = df.index[0]
Now I want to replace the cells with a list object(I know that storing complex objects in pandas columns is generally not a good practice) so I do:
df.loc[idx, 'temp'] = pd.Series([[1, 2, 3]], index=[idx])
#It is working fine and as expected
#This is for example..it has nothing to do with my actual question
but if I try to assign a list or nested list like this It will throw me an error(as expected):
df.loc[idx,'temp']=[1,2,3]
df.loc[idx,'temp']=[[1,2,3]]
df.loc[idx,'temp']=[[[1,2,3]]]
But Now If I try to assign a list of 3 or more dimensions with string in it:
df.loc[idx,'temp']=[[['1','2','3']]]
#It is working
#But If now if I assign a list of 3 or more dimensions with int in it after running the above code:
df.loc[idx,'temp']=[[[1,2,3]]]
#It is also working
So depending on the above observations(I am using python 3.9 and pandas version is '1.3.0')
My question:
what is this behaviour of loc accessor?