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.

DOC: DataFrame.update() fails to change values in calling dataframe if new value is NaN

See original GitHub issue

Code Sample, a copy-pastable example if possible

# Define dataframe
tempDF1 = pd.DataFrame({'c1':[1,2,3,4,5],
                       'c2':[True,True,False,False,True]})

# Select part of original dataframe
tempDF2 = tempDF1.loc[tempDF1['c2']==True,:].copy()

print('Original dataframe')
print(tempDF1)
print('\nSelection of dataframe')
print(tempDF2)

# Make some changes to selection
tempDF2.loc[:,'c1'] = tempDF2.loc[:,'c1'] + 100
tempDF2.iloc[2,0] = np.nan

# Update original dataframe with new values
tempDF1.update(tempDF2,overwrite=True)

print('\nSelection of dataframe - with changes')
print(tempDF2)
print('\nUpdated original dataframe')
print(tempDF1)

Problem description

DataFrame.update() function fails to update a dataframe with new NaN values. However, non-NaN values are updated to original dataframe with no issues (except the dtype of the dataframe is altered in the update process, namely int64 changed to float64).

Expected Output

Expected output would be

      c1     c2
0  101   True
1  102   True
2    3  False
3    4  False
4  NaN   True

However, actual output is:

      c1     c2
0  101.0   True
1  102.0   True
2    3.0  False
3    4.0  False
4    5.0   True

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None python: 3.4.6.final.0 python-bits: 64 OS: Darwin OS-release: 15.6.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: None LANG: en_GB.UTF-8 LOCALE: en_GB.UTF-8

pandas: 0.20.2 pytest: None pip: 9.0.1 setuptools: 34.3.3 Cython: None numpy: 1.13.0 scipy: None xarray: None IPython: 5.3.0 sphinx: None patsy: None dateutil: 2.6.0 pytz: 2017.2 blosc: None bottleneck: None tables: None numexpr: None feather: None matplotlib: None openpyxl: 2.4.7 xlrd: None xlwt: None xlsxwriter: None lxml: None bs4: None html5lib: 0.999999999 sqlalchemy: None pymysql: 0.7.11.None psycopg2: None jinja2: 2.9.6 s3fs: None pandas_gbq: None pandas_datareader: None

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
maxwellflittoncommented, Nov 19, 2018

Is there a parameter to track NaN values and change old DF values to NaN in the update function?

0reactions
joh-muecommented, Dec 18, 2018

Yes, what if I want to force NaN values in the update function?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Update a dataframe by dataframes with NaN values
I am late to the party but I was recently confronted to the same issue, i.e. trying to update a dataframe without ignoring...
Read more >
pandas.DataFrame.update — pandas 1.5.2 documentation
Modify in place using non-NA values from another DataFrame. Aligns on indices. There is no return value. Parameters. otherDataFrame, or object coercible ...
Read more >
Missing values in pandas (nan, None, pd.NA) - nkmk note
In addition to reading a file, nan is used to represent a missing value if the element does not exist when calling methods...
Read more >
Spark SQL, DataFrames and Datasets Guide
DataFrames can be constructed from a wide array of sources such as: structured data files, tables in Hive, external databases, or existing RDDs....
Read more >
Different Ways to Change Data Type in pandas
By default, when you are trying to change a column to a type that is not supported with the data, Pandas generates an...
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