# StringExpression to String

**URL:** <https://discuss.hail.is/t/stringexpression-to-string/2064>\
**Category:** Hail Query & hailctl\
**Created:** [June 2, 2021, 1:38am UTC](https://discuss.hail.is/t/stringexpression-to-string/2064 "2021-06-02T01:38:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mhebrard](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/mhebrard/32/322_2.png) [@mhebrard](https://discuss.hail.is/u/mhebrard)\
**Post date:** [June 2, 2021, 1:38am UTC](https://discuss.hail.is/t/stringexpression-to-string/2064/1 "2021-06-02T01:38:09Z")

</div>

Hi,

I got a python function that expect a string to perform some transformation on it.

```auto
def myfunction(str):
  return str.encode("utf-8").hex()

```

Eventually I wish to apply this function to one field of my hail table for each row

```auto
ht = ht.annotate(
    hex = myfunction(ht.s)
)

```

But I got an error :  
`AttributeError: 'StringExpression' object has no attribute 'encode'`

Is there a way to apply a function to each row ? how do I “cast” my StringExpression into the string of the current row ?

---

<div class="post-metadata">

**Author:** ![tpoterba](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/tpoterba/32/61_2.png) [@tpoterba](https://discuss.hail.is/u/tpoterba)\
**Post date:** [June 2, 2021, 1:46am UTC](https://discuss.hail.is/t/stringexpression-to-string/2064/2 "2021-06-02T01:46:46Z")

</div>

You’re asking about how to use user-defined functions in Hail. Unfortunately, this isn’t something we support – Hail’s backend is not running Python at all, and so it’s not possible to call `myfunction` from our backend. All of the functionality exposed on Hail expressions is implemented natively in Hail’s compiler / execution engine.

It’s pretty easy for us to expose this particular example (encode to binary, convert binary to hex strings). Would that be helpful?

---

<div class="post-metadata">

**Author:** ![mhebrard](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/mhebrard/32/322_2.png) [@mhebrard](https://discuss.hail.is/u/mhebrard)\
**Post date:** [June 2, 2021, 2:09am UTC](https://discuss.hail.is/t/stringexpression-to-string/2064/3 "2021-06-02T02:09:54Z")

</div>

I see,

I found a way to do my transformation:

- I select the field I need to transform
- Export to Pandas
- Here I am in python so I can apply my function
- Import table from Pandas
- Stich back my fields in my Hail Table

That is good enough for my usecase.  
Transformation on one hail table field do not required too many compute.  
Then I can use the power of Hail for more complicate genotype level handeling

Thanks
