Skip to content

Commit

Permalink
Change default name to "index"
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego committed Jan 8, 2024
1 parent 16b7492 commit c83565e
Show file tree
Hide file tree
Showing 23 changed files with 362 additions and 364 deletions.
2 changes: 1 addition & 1 deletion crates/polars-lazy/src/scan/parquet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ 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_index(&row_count.name, Some(row_count.offset))
}
Expand Down
18 changes: 9 additions & 9 deletions crates/polars-lazy/src/tests/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,30 +380,30 @@ fn test_row_count_on_files() -> PolarsResult<()> {

assert!(row_count_at_scan(lf.clone()));
let df = lf.collect()?;
let rn = df.column("index")?;
let idx = df.column("index")?;
assert_eq!(
rn.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_index("rn", Some(offset));
.with_row_index("index", Some(offset));
assert!(row_count_at_scan(lf.clone()));
let df = lf.collect()?;
let rn = df.column("rn")?;
let idx = df.column("index")?;
assert_eq!(
rn.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_index("rn", 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 rn = df.column("rn")?;
let idx = df.column("index")?;
assert_eq!(
rn.idx()?.into_no_null_iter().collect::<Vec<_>>(),
idx.idx()?.into_no_null_iter().collect::<Vec<_>>(),
(offset..27 + offset).collect::<Vec<_>>()
);

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_index("row_number", None)
.with_row_index("index", None)
.tail(5)
.collect()?;
let expected = df![
"row_number" => [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_index("row_number", None)
.with_row_index("index", None)
.slice(1, 2)
.collect()?;
assert_eq!(
out.column("row_number")?
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_index("row_number", None)
.with_row_index("index", None)
.filter(col("a").eq(lit(3i32)))
.collect()?;
assert_eq!(
out.column("row_number")?
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_index("row_number", None)
.with_row_index("index", None)
.collect()?;
assert_eq!(
out.column("row_number")?
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_index("row_number", None)
.with_row_index("index", None)
.collect()?;
assert_eq!(
out.column("row_number")?
out.column("index")?
.idx()?
.into_no_null_iter()
.collect::<Vec<_>>(),
Expand Down
6 changes: 3 additions & 3 deletions crates/polars-lazy/src/tests/projection_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ fn test_row_number_pd() -> PolarsResult<()> {

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

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

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_index("row_number", None)
.with_row_index("index", None)
.explode([col("tokens")])
.select([col("row_number"), 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_index("row_number", None)
.with_row_index("index", None)
.explode(["a"])
.group_by(["row_number"])
.group_by(["index"])
.agg([col("a").count().alias("count")])
.collect()?;

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

Ok(())
Expand Down
8 changes: 4 additions & 4 deletions docs/src/python/user-guide/expressions/column-selections.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
datetime(2022, 12, 1), datetime(2022, 12, 1, 0, 0, 2), "1s", eager=True
),
}
).with_row_index("rn")
).with_row_index("index")
print(df)
# --8<-- [end:selectors_df]

Expand All @@ -29,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 @@ -61,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_index("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
24 changes: 12 additions & 12 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5205,7 +5205,7 @@ def pipe(
"""
return function(self, *args, **kwargs)

def with_row_index(self, name: str = "row_number", offset: int = 0) -> Self:
def with_row_index(self, name: str = "index", offset: int = 0) -> Self:
"""
Add a column at index 0 that counts the rows.
Expand All @@ -5226,21 +5226,21 @@ def with_row_index(self, name: str = "row_number", offset: int = 0) -> Self:
... )
>>> df.with_row_index()
shape: (3, 3)
┌────────────┬─────┬─────┐
row_number ┆ a ┆ b │
│ --- ┆ --- ┆ --- │
│ u32 ┆ i64 ┆ i64 │
╞════════════╪═════╪═════╡
│ 0 ┆ 1 ┆ 2 │
│ 1 ┆ 3 ┆ 4 │
│ 2 ┆ 5 ┆ 6 │
└────────────┴─────┴─────┘
┌───────┬─────┬─────┐
index ┆ a ┆ b │
│ --- ┆ --- ┆ --- │
│ u32 ┆ i64 ┆ i64 │
╞═══════╪═════╪═════╡
│ 0 ┆ 1 ┆ 2 │
│ 1 ┆ 3 ┆ 4 │
│ 2 ┆ 5 ┆ 6 │
└───────┴─────┴─────┘
"""
return self._from_pydf(self._df.with_row_index(name, offset))

@deprecate_function(
"Use `with_row_index` instead."
"Note that the default column name has changed from 'row_nr' to 'row_number'.",
"Note that the default column name has changed from 'row_nr' to 'index'.",
version="0.20.4",
)
def with_row_count(self, name: str = "row_nr", offset: int = 0) -> Self:
Expand All @@ -5249,7 +5249,7 @@ def with_row_count(self, name: str = "row_nr", offset: int = 0) -> Self:
.. deprecated::
Use `meth`:with_row_index` instead.
Note that the default column name has changed from 'row_nr' to 'row_number'.
Note that the default column name has changed from 'row_nr' to 'index'.
Parameters
----------
Expand Down
Loading

0 comments on commit c83565e

Please sign in to comment.