SciRuby / daru

Data Analysis in RUby
BSD 2-Clause "Simplified" License
1.03k stars 139 forks source link

Add bang-version of Daru::DataFrame#rename_vectors for chaining calls #532

Closed rstammer closed 4 years ago

rstammer commented 4 years ago

Oftentimes when working with Daru's dataframes I encounter the problem that I want to do some "default" actions within a single chain of method calls, i.e. renaming the dataframe columns. Unfortunately, as Daru::DataFrame returns the new index set, one cannot chain the method calls, so the invocation of rename_vectors always needs a separate expression:

df = SomeCollection.to_df
df.rename_vectors(a: :alpha)
df.some_other_action!
df.another_operation
df

For convenience and compactness, it would help me to to such things in a more functional chaining manner like

df = 
  SomeCollection
    .to_df
    .rename_vectors!(a: :alpha)
    .some_other_action!
    .and_another_operation!
    .rename_vectors!(b: :beta)

Fortunately it's quite easy to achieve. As stated in the latter snippet, I added a sibling method for rename_vectors, namely, rename_vectors! that differs from rename_vectors by its return value. The new method returns the dataframe itself so that one can profit from chaining if one wants to.

Actually, in my work I never encountered the situation that the current return value of Daru::DataFrame#rename_vectors, the index set, is really of interest, so I first thought of just modifying the rename_vectors method itself. But even this would be still my favourite version I do not know if this would break other people's code - that's whyI'm not certain if this would be a good idea. What do you think?