Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inconsistent behavior when inserting a set into cells using .loc in pandas

Tags:

python

pandas

It's a pretty simple example

import pandas
df = pandas.DataFrame()
value_to_be_set = {'1'}

df.loc[0, 'col1'] = value_to_be_set
df['col2'] = None
df.loc[0, 'col2'] = value_to_be_set

print(df.head())

output

   col1 col2
0    1  {1}

Why is the datatype different for both columns?

Python 3.7.3
pandas version: 0.23.4

like image 436
Kaushik J Avatar asked Jun 11 '20 13:06

Kaushik J


1 Answers

In first assignment, you create a num_column from a set, said differently from an iterable. You ask for 1 single element and provide an iterable of size one, so you affect the content of the set to the single cell. You can try to use a set of 2 values to see that it would raise an error.

In second assignment, you update a cell in an existing column. Pandas has no reason to unpack anything here, and it affects the set to the cell.

To be honest, this explains what happens, but is not a justification for the rationale behind the different behaviours...

like image 123
Serge Ballesta Avatar answered Oct 21 '22 12:10

Serge Ballesta