pub fn may_apply_at_idx<F, S>(&mut self, idx: usize, f: F) -> Result<&mut Self>
    where
        F: FnOnce(&Series) -> Result<S>,
        S: IntoSeries,
    {
        let width = self.width();
        let col = self.columns.get_mut(idx).ok_or_else(|| {
            PolarsError::OutOfBounds(
                format!(
                    "Column index: {} outside of DataFrame with {} columns",
                    idx, width
                )
                .into(),
            )
        })?;
        let name = col.name().to_string();

        let _ = mem::replace(col, f(col).map(|s| s.into_series())?);

        // make sure the name remains the same after applying the closure
        unsafe {
            let col = self.columns.get_unchecked_mut(idx);
            col.rename(&name);
        }
        self.register_mutation()?;
        Ok(self)
    }
/// Apply a closure that may fail to a column at index `idx`. This is the recommended way to do in place
/// modification.
///
/// # Example
///
/// This is the idomatic way to replace some values a column of a `DataFrame` given range of indexes.
///
/// ```
/// # use polars::prelude::*;
/// let s0 = Series::new("foo", &["ham", "spam", "egg", "bacon", "quack"]);
/// let s1 = Series::new("values", &[1, 2, 3, 4, 5]);
/// let mut df = DataFrame::new(vec![s0, s1]).unwrap();
///
/// let idx = &[0, 1, 4];
///
/// df.may_apply("foo", |s| {
///     s.utf8()?
///     .set_at_idx_with(idx, |opt_val| opt_val.map(|string| format!("{}-is-modified", string)))
/// });
/// ```
/// Results in:
///
/// ```text
/// +---------------------+--------+
/// | foo                 | values |
/// | ---                 | ---    |
/// | str                 | i32    |
/// +=====================+========+
/// | "ham-is-modified"   | 1      |
/// +---------------------+--------+
/// | "spam-is-modified"  | 2      |
/// +---------------------+--------+
/// | "egg"               | 3      |
/// +---------------------+--------+
/// | "bacon"             | 4      |
/// +---------------------+--------+
/// | "quack-is-modified" | 5      |
/// +---------------------+--------+
/// ```
Apply a closure that may fail to a column at index `idx`. This is the recommended way to do in place
modification.
Apply a closure that may fail to a column at index ` idx ` . This is the recommended way to do in place modification .
pub fn may_apply_at_idx<F, S>(&mut self, idx: usize, f: F) -> Result<&mut Self>
    where
        F: FnOnce(&Series) -> Result<S>,
        S: IntoSeries,
    {
        let width = self.width();
        let col = self.columns.get_mut(idx).ok_or_else(|| {
            PolarsError::OutOfBounds(
                format!(
                    "Column index: {} outside of DataFrame with {} columns",
                    idx, width
                )
                .into(),
            )
        })?;
        let name = col.name().to_string();
        let _ = mem::replace(col, f(col).map(|s| s.into_series())?);
        // make sure the name remains the same after applying the closure
        unsafe {
            let col = self.columns.get_unchecked_mut(idx);
            col.rename(&name);
        }
        self.register_mutation()?;
        Ok(self)
    }
pub fn may_apply_at_idx < F , S > ( & mut self , idx : usize , f : F ) -> Result < & mut Self > where F : FnOnce ( & Series ) -> Result < S > , S : IntoSeries , { let width = self . width ( ) ; let col = self . columns . get_mut ( idx ) . ok_or_else ( | | { PolarsError :: OutOfBounds ( format ! ( " " idx width ) . into ( ) , ) } ) ? ; let name = col . name ( ) . to_string ( ) ; let _ = mem :: replace ( col , f ( col ) . map ( | s | s . into_series ( ) ) ? ) ; // make sure the name remains the same after applying the closure unsafe { let col = self . columns . get_unchecked_mut ( idx ) ; col . rename ( & name ) ; } self . register_mutation ( ) ? ; Ok ( self ) }
Apply a closure that may fail to a column at index `idx`.
Apply a closure that may fail to a column at index ` idx ` .
&mut self
idx
usize
f
F
&mut self
idx
usize
f
F
example
This is the idomatic way to replace some values a column of a `DataFrame` given range of indexes.


This is the idomatic way to replace some values a column of a ` DataFrame ` given range of indexes .
use polars::prelude::*;
None