# Inline function on transform

**URL:** <https://discourse.julialang.org/t/inline-function-on-transform/39176>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [May 9, 2020, 11:35am UTC](https://discourse.julialang.org/t/inline-function-on-transform/39176 "2020-05-09T11:35:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chedieck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chedieck/32/14716_2.png) [@chedieck](https://discourse.julialang.org/u/chedieck)\
**Post date:** [May 9, 2020, 11:35am UTC](https://discourse.julialang.org/t/inline-function-on-transform/39176/1 "2020-05-09T11:35:44Z")

</div>

So, I am using the `DataFrames` package and have this dataset called `salestrain` that has a column called `date`. Now date is a column of strings representing the date of the sales, each is written like `"12.10.2014"`; but I’m only interested in the month, in this particular example: `10`.

So I create a grouped dataframe:  
`gsalestrain = groupby(salestrain, :item_id)`

And now I try to add my desired month column:  
`salestrainmonth = transform(gsalestrain, :date => x -> split.(x, '.')[2])`

But that returns me an error:  
`ERROR: LoadError: BoundsError: attempt to access 1-element Array{Array{SubString{String},1},1} at index [2]`

Now, if I create an auxiliar function:  
`getmonth(x) = split(x,'.')[2]`

and try:

`salestrainmonth = transform(gsalestrain, :date => x -> getmonth.(x))`

it works fine.

I imagine that’s because the ‘dot notation’ is applied also with the index selection, right? My question is: is it really necessary to create and auxiliar function or is there a way to make it work with a inline function?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 9, 2020, 7:52pm UTC](https://discourse.julialang.org/t/inline-function-on-transform/39176/2 "2020-05-09T19:52:23Z")

</div>

The issue here is unrelated to `transform` or DataFrames:

```julia
julia> x = ["1.1.2000", "1.2.2000"]
2-element Array{String,1}:
 "1.1.2000"
 "1.2.2000"

julia> split.(x, ".")
2-element Array{Array{SubString{String},1},1}:
 ["1", "1", "2000"]
 ["1", "2", "2000"]

julia> split.(x, ".")[2]
3-element Array{SubString{String},1}:
 "1"
 "2"
 "2000"

julia> getmonth(x) = split(x, '.')[2]
getmonth (generic function with 1 method)

julia> getmonth.(x)
2-element Array{SubString{String},1}:
 "1"
 "2"

```

so the `getmonth` function splits its single argument and then takes the second element of the result, while `split.(x)` splits every element in `x` and therefore returns an array of arrays, each of which with containing the split results. So in `getmonth` you are indexing into the split result, while in `split.(x)[2]` you’re taking the second element of an array of arrays, which in itself is an array (the results of splitting the second argument of `x`, so `["1", "2", "2000"]` in my example).

What you therefore want is to also broadcast the selection of the second argument:

```julia
julia> getindex.(split.(x, '.'), 2)
2-element Array{SubString{String},1}:
 "1"
 "2"

```

Here, I’m using `getindex`, which is the function that `[]` brackets actually calls under the hood, and tell it to get the second index of each element in `split.(x, '.')`.
