Skip to content

Commit e1014c4

Browse files
committed
add ability to rename column with --rename-col arg on oxen df
1 parent a14650e commit e1014c4

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

src/cli/src/cmd/df.rs

+7
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,12 @@ impl RunCmd for DFCmd {
219219
.help("Add a row and cast to the values data types to match the current schema. If used with --add-col, row is added first, then column. Format 'comma,separated,vals'")
220220
.action(clap::ArgAction::Set),
221221
)
222+
.arg(
223+
Arg::new("rename-col")
224+
.long("rename-col")
225+
.help("Rename a column in the data frame. Format: 'old_name:new_name'")
226+
.action(clap::ArgAction::Set),
227+
)
222228
.arg(
223229
Arg::new("delete-row")
224230
.long("delete-row")
@@ -286,6 +292,7 @@ impl DFCmd {
286292
liboxen::opts::DFOpts {
287293
add_col: args.get_one::<String>("add-col").map(String::from),
288294
add_row: args.get_one::<String>("add-row").map(String::from),
295+
rename_col: args.get_one::<String>("rename-col").map(String::from),
289296
at: args
290297
.get_one::<String>("at")
291298
.map(|x| x.parse::<usize>().expect("at must be valid int")),

src/lib/src/core/df/tabular.rs

+28
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,21 @@ pub fn transform_lazy(mut df: LazyFrame, opts: DFOpts) -> Result<LazyFrame, Oxen
498498
}
499499
}
500500

501+
if let Some(names) = &opts.rename_col {
502+
if names.contains(":") {
503+
let parts = names.split(":").collect::<Vec<&str>>();
504+
let old_name = parts[0];
505+
let new_name = parts[1];
506+
let mut mut_df = df
507+
.collect()
508+
.map_err(|e| OxenError::basic_str(format!("{e:?}")))?;
509+
rename_col(&mut mut_df, old_name, new_name)?;
510+
df = mut_df.lazy();
511+
} else {
512+
log::error!("Invalid rename_col format: {}", names);
513+
}
514+
}
515+
501516
// These ops should be the last ops since they depends on order
502517
if let Some(indices) = opts.take_indices() {
503518
match take(df.clone(), indices) {
@@ -604,6 +619,19 @@ fn slice(df: LazyFrame, opts: &DFOpts) -> LazyFrame {
604619
}
605620
}
606621

622+
fn rename_col(
623+
df: &mut DataFrame,
624+
old_name: impl AsRef<str>,
625+
new_name: impl AsRef<str>,
626+
) -> Result<(), OxenError> {
627+
let old_name = old_name.as_ref();
628+
let new_name = new_name.as_ref();
629+
log::debug!("Renaming column {:?} to {:?}", old_name, new_name);
630+
df.rename(old_name, PlSmallStr::from_str(new_name))
631+
.map_err(|e| OxenError::basic_str(format!("{e:?}")))?;
632+
Ok(())
633+
}
634+
607635
pub fn df_add_row_num(df: DataFrame) -> Result<DataFrame, OxenError> {
608636
df.with_row_index(PlSmallStr::from_str(constants::ROW_NUM_COL_NAME), Some(0))
609637
.map_err(|e| OxenError::basic_str(format!("{e:?}")))

src/lib/src/opts/df_opts.rs

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub struct IndexedItem {
3131
pub struct DFOpts {
3232
pub add_col: Option<String>,
3333
pub add_row: Option<String>,
34+
pub rename_col: Option<String>,
3435
pub at: Option<usize>,
3536
pub columns: Option<String>,
3637
pub delete_row: Option<String>,
@@ -80,6 +81,7 @@ impl DFOpts {
8081
DFOpts {
8182
add_col: None,
8283
add_row: None,
84+
rename_col: None,
8385
at: None,
8486
columns: None,
8587
delete_row: None,
@@ -159,6 +161,7 @@ impl DFOpts {
159161
pub fn has_transform(&self) -> bool {
160162
self.add_col.is_some()
161163
|| self.add_row.is_some()
164+
|| self.rename_col.is_some()
162165
|| self.item.is_some()
163166
|| self.columns.is_some()
164167
|| self.filter.is_some()

src/lib/src/repositories/workspaces.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,7 @@ pub fn list(repo: &LocalRepository) -> Result<Vec<Workspace>, OxenError> {
259259
let workspaces_hashes = util::fs::list_dirs_in_dir(&workspaces_dir)
260260
.map_err(|e| OxenError::basic_str(format!("Error listing workspace directories: {}", e)))?;
261261

262-
log::debug!(
263-
"workspace::list got {} workspaces",
264-
workspaces_hashes.len()
265-
);
262+
log::debug!("workspace::list got {} workspaces", workspaces_hashes.len());
266263

267264
let mut workspaces = Vec::new();
268265
for workspace_hash in workspaces_hashes {

0 commit comments

Comments
 (0)