Skip to content

Commit

Permalink
depr(python,rust!): Rename with_row_count to with_row_index (#13494)
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego authored Jan 8, 2024
1 parent 1b818f0 commit 3077ba5
Show file tree
Hide file tree
Showing 40 changed files with 568 additions and 409 deletions.
4 changes: 2 additions & 2 deletions crates/polars-core/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ impl DataFrame {
/// let df1: DataFrame = df!("Name" => &["James", "Mary", "John", "Patricia"])?;
/// assert_eq!(df1.shape(), (4, 1));
///
/// let df2: DataFrame = df1.with_row_count("Id", None)?;
/// let df2: DataFrame = df1.with_row_index("Id", None)?;
/// assert_eq!(df2.shape(), (4, 2));
/// println!("{}", df2);
///
Expand All @@ -369,7 +369,7 @@ impl DataFrame {
/// | 3 | Patricia |
/// +-----+----------+
/// ```
pub fn with_row_count(&self, name: &str, offset: Option<IdxSize>) -> PolarsResult<Self> {
pub fn with_row_index(&self, name: &str, offset: Option<IdxSize>) -> PolarsResult<Self> {
let mut columns = Vec::with_capacity(self.columns.len() + 1);
let offset = offset.unwrap_or(0);

Expand Down
2 changes: 1 addition & 1 deletion crates/polars-lazy/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1642,7 +1642,7 @@ impl LazyFrame {
/// # Warning
/// This can have a negative effect on query performance. This may for instance block
/// predicate pushdown optimization.
pub fn with_row_count(mut self, name: &str, offset: Option<IdxSize>) -> LazyFrame {
pub fn with_row_index(mut self, name: &str, offset: Option<IdxSize>) -> LazyFrame {
let add_row_count_in_map = match &mut self.logical_plan {
LogicalPlan::Scan {
file_options: options,
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-lazy/src/scan/anonymous_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl LazyFrame {
.into();

if let Some(rc) = args.row_count {
lf = lf.with_row_count(&rc.name, Some(rc.offset))
lf = lf.with_row_index(&rc.name, Some(rc.offset))
};

Ok(lf)
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-lazy/src/scan/file_list_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub trait LazyFileListReader: Clone {
lf = lf.slice(0, n_rows as IdxSize)
};
if let Some(rc) = self.row_count() {
lf = lf.with_row_count(&rc.name, Some(rc.offset))
lf = lf.with_row_index(&rc.name, Some(rc.offset))
};

Ok(lf)
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-lazy/src/scan/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl LazyFileListReader for LazyIpcReader {

// it is a bit hacky, but this row_count function updates the schema
if let Some(row_count) = args.row_count {
lf = lf.with_row_count(&row_count.name, Some(row_count.offset))
lf = lf.with_row_index(&row_count.name, Some(row_count.offset))
}

Ok(lf)
Expand Down
4 changes: 2 additions & 2 deletions crates/polars-lazy/src/scan/parquet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ impl LazyFileListReader for LazyParquetReader {
.build()
.into();

// it is a bit hacky, but this row_count function updates the schema
// it is a bit hacky, but this row_index function updates the schema
if let Some(row_count) = row_count {
lf = lf.with_row_count(&row_count.name, Some(row_count.offset))
lf = lf.with_row_index(&row_count.name, Some(row_count.offset))
}

lf.opt_state.file_caching = true;
Expand Down
22 changes: 11 additions & 11 deletions crates/polars-lazy/src/tests/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,42 +373,42 @@ fn test_row_count_on_files() -> PolarsResult<()> {
for offset in [0 as IdxSize, 10] {
let lf = LazyCsvReader::new(FOODS_CSV)
.with_row_count(Some(RowCount {
name: "rc".into(),
name: "index".into(),
offset,
}))
.finish()?;

assert!(row_count_at_scan(lf.clone()));
let df = lf.collect()?;
let rc = df.column("rc")?;
let idx = df.column("index")?;
assert_eq!(
rc.idx()?.into_no_null_iter().collect::<Vec<_>>(),
idx.idx()?.into_no_null_iter().collect::<Vec<_>>(),
(offset..27 + offset).collect::<Vec<_>>()
);

let lf = LazyFrame::scan_parquet(FOODS_PARQUET, Default::default())?
.with_row_count("rc", Some(offset));
.with_row_index("index", Some(offset));
assert!(row_count_at_scan(lf.clone()));
let df = lf.collect()?;
let rc = df.column("rc")?;
let idx = df.column("index")?;
assert_eq!(
rc.idx()?.into_no_null_iter().collect::<Vec<_>>(),
idx.idx()?.into_no_null_iter().collect::<Vec<_>>(),
(offset..27 + offset).collect::<Vec<_>>()
);

let lf =
LazyFrame::scan_ipc(FOODS_IPC, Default::default())?.with_row_count("rc", Some(offset));
let lf = LazyFrame::scan_ipc(FOODS_IPC, Default::default())?
.with_row_index("index", Some(offset));

assert!(row_count_at_scan(lf.clone()));
let df = lf.clone().collect()?;
let rc = df.column("rc")?;
let idx = df.column("index")?;
assert_eq!(
rc.idx()?.into_no_null_iter().collect::<Vec<_>>(),
idx.idx()?.into_no_null_iter().collect::<Vec<_>>(),
(offset..27 + offset).collect::<Vec<_>>()
);

let out = lf
.filter(col("rc").gt(lit(-1)))
.filter(col("index").gt(lit(-1)))
.select([col("calories")])
.collect()?;
assert!(out.column("calories").is_ok());
Expand Down
20 changes: 10 additions & 10 deletions crates/polars-lazy/src/tests/optimization_checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,23 +351,23 @@ fn test_with_row_count_opts() -> PolarsResult<()> {
let out = df
.clone()
.lazy()
.with_row_count("row_nr", None)
.with_row_index("index", None)
.tail(5)
.collect()?;
let expected = df![
"row_nr" => [5 as IdxSize, 6, 7, 8, 9],
"index" => [5 as IdxSize, 6, 7, 8, 9],
"a" => [5, 6, 7, 8, 9],
]?;

assert!(out.equals(&expected));
let out = df
.clone()
.lazy()
.with_row_count("row_nr", None)
.with_row_index("index", None)
.slice(1, 2)
.collect()?;
assert_eq!(
out.column("row_nr")?
out.column("index")?
.idx()?
.into_no_null_iter()
.collect::<Vec<_>>(),
Expand All @@ -377,11 +377,11 @@ fn test_with_row_count_opts() -> PolarsResult<()> {
let out = df
.clone()
.lazy()
.with_row_count("row_nr", None)
.with_row_index("index", None)
.filter(col("a").eq(lit(3i32)))
.collect()?;
assert_eq!(
out.column("row_nr")?
out.column("index")?
.idx()?
.into_no_null_iter()
.collect::<Vec<_>>(),
Expand All @@ -392,10 +392,10 @@ fn test_with_row_count_opts() -> PolarsResult<()> {
.clone()
.lazy()
.slice(1, 2)
.with_row_count("row_nr", None)
.with_row_index("index", None)
.collect()?;
assert_eq!(
out.column("row_nr")?
out.column("index")?
.idx()?
.into_no_null_iter()
.collect::<Vec<_>>(),
Expand All @@ -405,10 +405,10 @@ fn test_with_row_count_opts() -> PolarsResult<()> {
let out = df
.lazy()
.filter(col("a").eq(lit(3i32)))
.with_row_count("row_nr", None)
.with_row_index("index", None)
.collect()?;
assert_eq!(
out.column("row_nr")?
out.column("index")?
.idx()?
.into_no_null_iter()
.collect::<Vec<_>>(),
Expand Down
8 changes: 4 additions & 4 deletions crates/polars-lazy/src/tests/projection_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,20 @@ fn test_cross_join_pd() -> PolarsResult<()> {
}

#[test]
fn test_row_count_pd() -> PolarsResult<()> {
fn test_row_number_pd() -> PolarsResult<()> {
let df = df![
"x" => [1, 2, 3],
"y" => [3, 2, 1],
]?;

let df = df
.lazy()
.with_row_count("row_count", None)
.select([col("row_count"), col("x") * lit(3i32)])
.with_row_index("index", None)
.select([col("index"), col("x") * lit(3i32)])
.collect()?;

let expected = df![
"row_count" => [0 as IdxSize, 1, 2],
"index" => [0 as IdxSize, 1, 2],
"x" => [3i32, 6, 9]
]?;

Expand Down
2 changes: 1 addition & 1 deletion crates/polars-plan/src/logical_plan/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ impl FunctionNode {
let args = (**args).clone();
df.melt2(args)
},
RowCount { name, offset, .. } => df.with_row_count(name.as_ref(), *offset),
RowCount { name, offset, .. } => df.with_row_index(name.as_ref(), *offset),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/polars/tests/it/lazy/explodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ fn test_explode_row_numbers() -> PolarsResult<()> {
]?
.lazy()
.select([col("text").str().split(lit(" ")).alias("tokens")])
.with_row_count("row_nr", None)
.with_row_index("index", None)
.explode([col("tokens")])
.select([col("row_nr"), col("tokens")])
.select([col("index"), col("tokens")])
.collect()?;

assert_eq!(df.shape(), (8, 2));
Expand Down
6 changes: 3 additions & 3 deletions crates/polars/tests/it/lazy/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ fn test_sorted_path() -> PolarsResult<()> {

let out = df
.lazy()
.with_row_count("row_nr", None)
.with_row_index("index", None)
.explode(["a"])
.group_by(["row_nr"])
.group_by(["index"])
.agg([col("a").count().alias("count")])
.collect()?;

let s = out.column("row_nr")?;
let s = out.column("index")?;
assert_eq!(s.is_sorted_flag(), IsSorted::Ascending);

Ok(())
Expand Down
13 changes: 6 additions & 7 deletions docs/src/python/user-guide/expressions/column-selections.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# --8<-- [start:setup]
import polars as pl

# --8<-- [end:setup]

# --8<-- [start:selectors_df]
from datetime import date, datetime

import polars as pl

df = pl.DataFrame(
{
"id": [9, 4, 2],
Expand All @@ -17,7 +16,7 @@
datetime(2022, 12, 1), datetime(2022, 12, 1, 0, 0, 2), "1s", eager=True
),
}
).with_row_count("rn")
).with_row_index("index")
print(df)
# --8<-- [end:selectors_df]

Expand All @@ -30,7 +29,7 @@
# --8<-- [end:all]

# --8<-- [start:exclude]
out = df.select(pl.col("*").exclude("logged_at", "rn"))
out = df.select(pl.col("*").exclude("logged_at", "index"))
print(out)
# --8<-- [end:exclude]

Expand Down Expand Up @@ -62,12 +61,12 @@
# --8<-- [end:selectors_diff]

# --8<-- [start:selectors_union]
out = df.select(cs.by_name("rn") | ~cs.numeric())
out = df.select(cs.by_name("index") | ~cs.numeric())
print(out)
# --8<-- [end:selectors_union]

# --8<-- [start:selectors_by_name]
out = df.select(cs.contains("rn"), cs.matches(".*_.*"))
out = df.select(cs.contains("index"), cs.matches(".*_.*"))
print(out)
# --8<-- [end:selectors_by_name]

Expand Down
4 changes: 2 additions & 2 deletions docs/src/rust/user-guide/expressions/column-selections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
"logged_at" => date_range("logged_at",
NaiveDate::from_ymd_opt(2022, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap(), NaiveDate::from_ymd_opt(2022, 1, 1).unwrap().and_hms_opt(0, 0, 2).unwrap(), Duration::parse("1s"),ClosedWindow::Both, TimeUnit::Milliseconds, None)?,
)?
.with_row_count("rn", None)?;
.with_row_index("index", None)?;
println!("{}", &df);
// --8<-- [end:selectors_df]

Expand All @@ -33,7 +33,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let out = df
.clone()
.lazy()
.select([col("*").exclude(["logged_at", "rn"])])
.select([col("*").exclude(["logged_at", "index"])])
.collect()?;
println!("{}", &out);
// --8<-- [end:exclude]
Expand Down
2 changes: 1 addition & 1 deletion py-polars/docs/source/reference/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Examples
self._df = df
def by_alternate_rows(self) -> list[pl.DataFrame]:
df = self._df.with_row_count(name="n")
df = self._df.with_row_index(name="n")
return [
df.filter((pl.col("n") % 2) == 0).drop("n"),
df.filter((pl.col("n") % 2) != 0).drop("n"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@ Manipulation/selection
DataFrame.with_columns
DataFrame.with_columns_seq
DataFrame.with_row_count
DataFrame.with_row_index
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ Manipulation/selection
LazyFrame.with_columns_seq
LazyFrame.with_context
LazyFrame.with_row_count
LazyFrame.with_row_index
Loading

0 comments on commit 3077ba5

Please sign in to comment.