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: DafaFrame.insert doesn't raise an exception when inserting another DataFrame

See original GitHub issue
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

import pandas
df = pandas.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
df.insert(1, "newcol", df)
df
   col1 newcol  col2
0     1   col1     3
1     2   col2     4

Problem description

Dataframe should take int, Series, or array-like as a value and throw an exception in case DataFrame is passed.

Expected Output

Behavior in 1.2.5

import pandas
df = pandas.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
df.insert(1, "newcol", df)
ValueError: Wrong number of items passed 2, placement implies 1

Output of pd.show_versions()

pandas : 1.3.0 numpy : 1.20.3 pytz : 2020.1 dateutil : 2.8.1 pip : 21.1.2 setuptools : 49.6.0.post20210108 Cython : None pytest : 6.2.4 hypothesis : None sphinx : 4.0.2 blosc : None feather : 0.4.1 xlsxwriter : None lxml.etree : 4.6.3 html5lib : None pymysql : None psycopg2 : None jinja2 : 3.0.1 IPython : 7.24.1 pandas_datareader: None bs4 : 4.9.3 bottleneck : None fsspec : 2021.05.0 fastparquet : None gcsfs : None matplotlib : 3.2.2 numexpr : 2.7.3 odfpy : None openpyxl : 3.0.7 pandas_gbq : 0.15.0 pyarrow : 3.0.0 pyxlsb : None s3fs : None scipy : 1.6.3 sqlalchemy : 1.4.18 tables : 3.6.1 tabulate : None xarray : 0.18.2 xlrd : 2.0.1 xlwt : None numba : None

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jrebackcommented, Jul 29, 2021

we should have an explicit test for this

0reactions
YarShevcommented, Jul 29, 2021

Feel free to close the issue because I see expected behavior of the operation to me since 1.3.1.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Pandas Inserting into a DF Not Working - Stack Overflow
insert is an in-place operation, which returns None , you are assigning None to your DataFrame, just remove the assignment and you will...
Read more >
error handling; pandas and data analysis
we've already seen the raise keyword, in passing; raise Exception is the ... x = -1 if not isinstance(x,str): ## check if x...
Read more >
pandas.DataFrame.insert — pandas 1.5.2 documentation
Insert column into DataFrame at specified location. Raises a ValueError if column is already contained in the DataFrame, unless allow_duplicates is set to...
Read more >
Solve Pandas "ValueError: cannot reindex from a duplicate axis"
Apparently, the python error is the result of doing operations on a DataFrame that has duplicate index values. Operations that require unique ...
Read more >
Spark SQL, DataFrames and Datasets Guide
To use these features, you do not need to have an existing Hive setup. ... Create another DataFrame in a new partition directory,...
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