Skip to content

[SPARK-14932][SQL] Allow DataFrame.replace() to replace values with None - #16225

Closed
jiayue-zhang wants to merge 8 commits into
apache:masterfrom
jiayue-zhang:spark-14932
Closed

[SPARK-14932][SQL] Allow DataFrame.replace() to replace values with None#16225
jiayue-zhang wants to merge 8 commits into
apache:masterfrom
jiayue-zhang:spark-14932

Conversation

@jiayue-zhang

@jiayue-zhangjiayue-zhang commented Dec 9, 2016

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Allow DataFrame.replace() to replace with None/null values.

How was this patch tested?

Python doctest and unit test. Scala unit test.

@HyukjinKwon

Copy link
Copy Markdown
Member

Would we need a test for Scala too? I checked this by myself and it seems working fine with Scala though. I could argue that this affects the language-specific functions of both Python and Scala as a not strong opinion.

case _ => replacement.map { case (k, v) => (convertToDouble(k), convertToDouble(v)) }
}
val replacementMap: Map[_, _] =
if (replacement.head._2 == null)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can just write case null => here and thereby avoid the ifelse

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that but failed. Scala doesn't allow pattern matching with null, scala.Nothing and scala.Null

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, wait case null => works. I tried case v null which doesn't. Let me modify this. Thanks!

@SparkQA

Copy link
Copy Markdown

Test build #3490 has finished for PR 16225 at commit 0b15c8f.

  • This patch fails Python style tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@gatorsmile

Copy link
Copy Markdown
Member

ok to test

@gatorsmile

Copy link
Copy Markdown
Member

@bravo-zhang Could you resolve the conflicts? I will review it then. Thanks!

@SparkQA

Copy link
Copy Markdown

Test build #77261 has finished for PR 16225 at commit 2c532c3.

  • This patch passes all tests.
  • This patch does not merge cleanly.
  • This patch adds no public classes.

@jiayue-zhang

Copy link
Copy Markdown
ContributorAuthor

Thanks for taking a look, @gatorsmile The conflicts have been resolved.
I appreciate if @zero323 can take a look as well since you made improvement on this function recently.

@SparkQA

Copy link
Copy Markdown

Test build #77273 has finished for PR 16225 at commit 43fb6bd.

  • This patch fails PySpark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA

Copy link
Copy Markdown

Test build #77282 has finished for PR 16225 at commit b5424d9.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@holdenkholdenk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this, sorry its taken so long to review. I did a first read through the Python side and I've got two minor questions. Hopefully @zero323, @davie , or @gatorsmile can also have some time once the current release is finished to take a look.

"Got {0}".format(type(to_replace)))

if not isinstance(value, valid_types) and not isinstance(to_replace, dict):
if not isinstance(value, valid_types) and value is not None \

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So slightly jet lagged style question: would this be clearer if we just add type(None) to L1398? I know PEP8 says we should only use isis not for checking if something is none rather than depending on the implicit conversion to boolean -- but since really checking the type here we aren't really in danger of that. (This is just a suggestion to make it easier to read - if others think its easier to read this way thats fine :)). @davies ?

to_replace = [to_replace]

if isinstance(value, (float, int, long, basestring)):
if isinstance(value, (float, int, long, basestring)) or value is None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

if not any(all_of_type(rep_dict.keys()) and all_of_type(rep_dict.values())
if not any(all_of_type(rep_dict.keys())
and (all_of_type(rep_dict.values())
or list(rep_dict.values()).count(None) == len(rep_dict))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the Scala code null is allowed in to be the replacement value for some of the elements but not in the Python code. Is this intentional? If so we should document it clearly and expand on the error message bellow (otherwise we should make it more flexible).

@HyukjinKwonHyukjinKwon mentioned this pull request Jun 25, 2017
@gatorsmile

Copy link
Copy Markdown
Member

We are closing it due to inactivity. please do reopen if you want to push it forward. Thanks!

@jiayue-zhang

Copy link
Copy Markdown
ContributorAuthor

@holdenk Thanks for review. I'll combine type(None) in the isinstance. I also made Scala and Python to accept null more generally and in the same way.
PR is reopened at: #18820
@gatorsmile please also take a look.

zifeif2 pushed a commit to zifeif2/spark that referenced this pull request Nov 22, 2025
## What changes were proposed in this pull request?
This PR proposes to close stale PRs, mostly the same instances with apache#18017
I believe the author in apache#14807 removed his account.
Closesapache#7075Closesapache#8927Closesapache#9202Closesapache#9366Closesapache#10861Closesapache#11420Closesapache#12356Closesapache#13028Closesapache#13506Closesapache#14191Closesapache#14198Closesapache#14330Closesapache#14807Closesapache#15839Closesapache#16225Closesapache#16685Closesapache#16692Closesapache#16995Closesapache#17181Closesapache#17211Closesapache#17235Closesapache#17237Closesapache#17248Closesapache#17341Closesapache#17708Closesapache#17716Closesapache#17721Closesapache#17937
Added:
Closesapache#14739Closesapache#17139Closesapache#17445Closesapache#18042Closesapache#18359
Added:
Closesapache#16450Closesapache#16525Closesapache#17738
Added:
Closesapache#16458Closesapache#16508Closesapache#17714
Added:
Closesapache#17830Closesapache#14742
## How was this patch tested?
N/A
Author: hyukjinkwon <gurwls223@gmail.com>
Closesapache#18417 from HyukjinKwon/close-stale-pr.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@jiayue-zhang@HyukjinKwon@SparkQA@gatorsmile@JoshRosen@holdenk