# Dealing with missing values in RDataFrame(CSV)

**URL:** <https://root-forum.cern.ch/t/dealing-with-missing-values-in-rdataframe-csv/37362>\
**Category:** Newbie\
**Tags:** rdataframe\
**Created:** [December 29, 2019, 8:05am UTC](https://root-forum.cern.ch/t/dealing-with-missing-values-in-rdataframe-csv/37362 "2019-12-29T08:05:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![marty1885](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/marty1885/32/19232_2.png) [@marty1885](https://root-forum.cern.ch/u/marty1885)\
**Post date:** [December 29, 2019, 8:05am UTC](https://root-forum.cern.ch/t/dealing-with-missing-values-in-rdataframe-csv/37362/1 "2019-12-29T08:05:53Z")

</div>

Hi, what’s the proper way to deal with missing values in a RDataFrame?

For example, consider the training data if the [Tianic dataset](https://www.kaggle.com/c/titanic/data). Even though ROOT thinks the column `Age` is a number, trying to plot a histogram of `Age` will result in an exception due to missing values.

```c++
root [] rdf = ROOT::RDF::MakeCsvDataFrame("train.csv");
root [] for(auto name : rdf.GetColumnNames()) {
root [] cout << "\t"<< name << " " << rdf.GetColumnType(name) << "\n";
root [] }
	PassengerId Long64_t
	Survived Long64_t
	Pclass Long64_t
	Name std::string
	Sex std::string
	Age Long64_t
	SibSp Long64_t
	Parch Long64_t
	Ticket std::string
	Fare double
	Cabin std::string
	 std::string
root [] h1 = rdf.Histo1D("Age")
(ROOT::RDF::RResultPtr<TH1D> &) @0x7efbff6a1050
root [] h1->Draw();
Error in <TRint::HandleTermInput()>: std::invalid_argument caught: stoll

```

I know this is just an example. Yet how should we deal with this in practice?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [December 29, 2019, 8:37pm UTC](https://root-forum.cern.ch/t/dealing-with-missing-values-in-rdataframe-csv/37362/2 "2019-12-29T20:37:10Z")

</div>

From the error message, I suspect your “train.csv” file contains data lines which are not properly parsed by ROOT (you could attach this file for inspection).

---

<div class="post-metadata">

**Author:** ![marty1885](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/marty1885/32/19232_2.png) [@marty1885](https://root-forum.cern.ch/u/marty1885)\
**Post date:** [December 30, 2019, 3:28am UTC](https://root-forum.cern.ch/t/dealing-with-missing-values-in-rdataframe-csv/37362/3 "2019-12-30T03:28:14Z")

</div>

Hi, thanks. The forums doesn’t support uploading CSV files. So I have a link above to the train.csv file. Here’s it in txt form[train.txt](https://root-forum.cern.ch/uploads/short-url/xomntSW87XceiF5pM1l7u99csMA.txt) (59.8 KB)

> [@Wile\_E\_Coyote](#):
>
> file contains data lines which are not properly parsed by ROOT

Yes, that’s the exact problem I described. ROOT failed to parse the file due to missing values.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [December 30, 2019, 9:07am UTC](https://root-forum.cern.ch/t/dealing-with-missing-values-in-rdataframe-csv/37362/4 "2019-12-30T09:07:21Z")

</div>

@etejedor There is a bug in ROOT’s “csv” parsing. It improperly parses “DOS” encoded files. You can see it with the attached file, in the very last column name. It should be “Embarked” but ROOT improperly uses the final “carriage return” character as the last character of this name (this character does not appear in “Unix” encoded files, of course).

@marty1885 The problem is that the “Age” and “Cabin” columns are sometimes empty. ROOT cannot accept it. When creating this file, you would need to make sure that every entry gets some “default” value.

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [December 30, 2019, 11:21am UTC](https://root-forum.cern.ch/t/dealing-with-missing-values-in-rdataframe-csv/37362/5 "2019-12-30T11:21:43Z")

</div>

Hi,  
this is a known missing feature, relevant discussion + link to the jira ticket is [here](https://root-forum.cern.ch/t/nan-values-in-one-column-break-histogram-of-another-column/37091).

Unfortunately, at the moment we do not have free hands to implement support for missing values (me myself will not be at CERN until March) but PRs are of course welcome – or comments to the jira ticket explaining why the feature should float up the to do list.

The simplest workaround is to substitute missing values with some telltale value.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![marty1885](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/marty1885/32/19232_2.png) [@marty1885](https://root-forum.cern.ch/u/marty1885)\
**Post date:** [December 31, 2019, 3:07am UTC](https://root-forum.cern.ch/t/dealing-with-missing-values-in-rdataframe-csv/37362/6 "2019-12-31T03:07:03Z")

</div>

Sounds like something I can do, hopefully. Would I be able to submit a PR if I don’t work at CERN?

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [January 2, 2020, 3:39pm UTC](https://root-forum.cern.ch/t/dealing-with-missing-values-in-rdataframe-csv/37362/7 "2020-01-02T15:39:59Z")

</div>

> [@marty1885](#):
>
> Would I be able to submit a PR if I don’t work at CERN?

Absolutely, ROOT is open source and welcomes external contributions. You can find the code at [GitHub - root-project/root: The official repository for ROOT: analyzing, storing and visualizing big data, scientifically](https://github.com/root-project/root/).

I’d ask @etejedor, the author of the `RCSVDataSource`, to point to the parts of the code that need upgrading to support missing values. There is also the question of what behavior we want RDF to have when there is a missing value in a CSV file.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [January 7, 2020, 10:44am UTC](https://root-forum.cern.ch/t/dealing-with-missing-values-in-rdataframe-csv/37362/8 "2020-01-07T10:44:50Z")

</div>

Hi,

Regarding the DOS format problem, please see my answer here:

> [@Plot of nested beans](https://root-forum.cern.ch/t/plot-of-nested-beans/35951/13):
>
> Hi, The issue is that the .txt file was generated in Windows. When reading from the file in Linux, std::getline returns a string ended in \r. That \r is parsed as part of the second column, and therefore the inferred type is string. You can convert your Windows file to unix format with e.g. dos2unix. Also, I noticed that the first row contains a 0 in the second column. Provided that you convert the file to unix format, and since it is the first row that is used to infer the types, the second c…

As for the RCsvDataSource, this is the relevant file in ROOT:

> <https://github.com/root-project/root/blob/master/tree/dataframe/src/RCsvDS.cxx>

Possibly it should be enough to add the support for the empty values in `ParseValue`
