# How to specify RDF.Snapshot() column types in PyROOT?

**URL:** <https://root-forum.cern.ch/t/how-to-specify-rdf-snapshot-column-types-in-pyroot/33935>\
**Category:** ROOT\
**Tags:** python, rdataframe\
**Created:** [May 7, 2019, 11:07am UTC](https://root-forum.cern.ch/t/how-to-specify-rdf-snapshot-column-types-in-pyroot/33935 "2019-05-07T11:07:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_Charman](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/thomas_charman/32/19394_2.png) [@Thomas\_Charman](https://root-forum.cern.ch/u/Thomas_Charman)\
**Post date:** [May 7, 2019, 11:07am UTC](https://root-forum.cern.ch/t/how-to-specify-rdf-snapshot-column-types-in-pyroot/33935/1 "2019-05-07T11:07:32Z")

</div>

Hi,

I can see that in C++ ROOT one can specify the type different columns should have when writing them to disk with Snapshot, what is the way to do this in pyroot?

e.g. in C++

```auto
// specifying template parameters ("x" is `int`, "y" is `float`)
df.Snapshot<int, float>("outputTree", "outputFile.root", {"x", "y"});

```

Thanks,  
Tom

---

<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:** [May 7, 2019, 12:36pm UTC](https://root-forum.cern.ch/t/how-to-specify-rdf-snapshot-column-types-in-pyroot/33935/2 "2019-05-07T12:36:23Z")

</div>

Hi,  
@etejedor can comment with more authority on the subject, but I don’t think that current PyROOT supports calling C++ template methods. So there is no straightforward way to do it.

Depending on why you need to specify the template parameters, it might or might not be worth it to create a helper function that does what you need.

Something like (haven’t tested this code):

```python
def create_snapshot_call(*types: str):
  call_str = "void CallSnapshot(ROOT::RDF::RNode df) { df.Snapshot<"
  # add template arguments to call_str
  for t in types:
    call_str += f"{t},"
  call_str = call_str[:-1] # remove last comma
  call_str += '>("outputTree", "outputFile.root", {"x", "y"})'
  ROOT.gInterpreter.Declare(call_str)

```

to be used as

```python
>>> create_snapshot_call("int", "float") # this creates ROOT.CallSnapshot
>>> ROOT.CallSnapshot(df)

```

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![Thomas\_Charman](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/thomas_charman/32/19394_2.png) [@Thomas\_Charman](https://root-forum.cern.ch/u/Thomas_Charman)\
**Post date:** [May 7, 2019, 12:50pm UTC](https://root-forum.cern.ch/t/how-to-specify-rdf-snapshot-column-types-in-pyroot/33935/3 "2019-05-07T12:50:59Z")

</div>

Hi @eguiraud

Thanks for the response.

This certain looks like a candidate solution to what I desire.

I wonder if perhaps there is an easy way given my use case… all I am intending to do is change the type of a column from an int to a float.

I can’t define a new column that simply type casts the data from the old column as I need the column to specifically have the name that the old column has and using a name that already exists in a TTree is not allowed I believe.

Nevertheless if there is no simpler way I will try your solution.

Thanks,  
Thomas

---

<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:** [May 7, 2019, 1:07pm UTC](https://root-forum.cern.ch/t/how-to-specify-rdf-snapshot-column-types-in-pyroot/33935/4 "2019-05-07T13:07:07Z")

</div>

Hi Thomas,

> [@Thomas\_Charman](#):
>
> all I am intending to do is change the type of a column from an int to a float.

Ah, this is embarrassing, RDataFrame is not able to do that (yet) 😅  
The `Snapshot` template parameters are for the type of the column in input, not for the type of the column in output (which is simply assumed to be the same as the one in input).

I don’t think there is a way to change the type (or the value) of a column without renaming it.  
If you require this feature, please open a feature request at [Loading...](https://sft.its.cern.ch/jira/projects/ROOT).

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![Thomas\_Charman](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/thomas_charman/32/19394_2.png) [@Thomas\_Charman](https://root-forum.cern.ch/u/Thomas_Charman)\
**Post date:** [May 8, 2019, 11:43am UTC](https://root-forum.cern.ch/t/how-to-specify-rdf-snapshot-column-types-in-pyroot/33935/5 "2019-05-08T11:43:48Z")

</div>

Hi Enrico,

The embarrassment is not yours, rather that TMVA Reader is not happy with integers at least in [this case](https://root-forum.cern.ch/t/tmva-category/16003).

I am not sure if this is really a necessary feature of RDataFrames, it does seem to me that perhaps one should just use the correct type in the first place, or type cast whatever is being read when you need it (this is what I did in the end).

If it turns out I find a use for this feature in the future I’ll open a feature request. In terms of this thread I think your previous answer does answer the question in terms of how to take that C++ snippet and do something similar in Python (despite my misunderstanding of what the code was doing in the first place.)

Thanks,  
Thomas

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [May 22, 2019, 11:43am UTC](https://root-forum.cern.ch/t/how-to-specify-rdf-snapshot-column-types-in-pyroot/33935/6 "2019-05-22T11:43:52Z")

</div>

Closed on May 22, 2019, 11:43am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
