Function polars_core::frame::_duplicate_err
source · pub fn _duplicate_err(name: &str) -> PolarsResult<()>Examples found in repository?
src/frame/mod.rs (line 256)
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
pub fn new<S: IntoSeries>(columns: Vec<S>) -> PolarsResult<Self> {
let mut first_len = None;
let shape_err = |s: &[Series]| {
let msg = format!(
"Could not create a new DataFrame from Series. \
The Series have different lengths. \
Got {s:?}",
);
Err(PolarsError::ShapeMisMatch(msg.into()))
};
let series_cols = if S::is_series() {
// Safety:
// we are guarded by the type system here.
#[allow(clippy::transmute_undefined_repr)]
let series_cols = unsafe { std::mem::transmute::<Vec<S>, Vec<Series>>(columns) };
let mut names = PlHashSet::with_capacity(series_cols.len());
for s in &series_cols {
match first_len {
Some(len) => {
if s.len() != len {
return shape_err(&series_cols);
}
}
None => first_len = Some(s.len()),
}
let name = s.name();
if names.contains(name) {
_duplicate_err(name)?
}
names.insert(name);
}
// we drop early as the brchk thinks the &str borrows are used when calling the drop
// of both `series_cols` and `names`
drop(names);
series_cols
} else {
let mut series_cols = Vec::with_capacity(columns.len());
let mut names = PlHashSet::with_capacity(columns.len());
// check for series length equality and convert into series in one pass
for s in columns {
let series = s.into_series();
match first_len {
Some(len) => {
if series.len() != len {
return shape_err(&series_cols);
}
}
None => first_len = Some(series.len()),
}
// we have aliasing borrows so we must allocate a string
let name = series.name().to_string();
if names.contains(&name) {
_duplicate_err(&name)?
}
series_cols.push(series);
names.insert(name);
}
drop(names);
series_cols
};
Ok(DataFrame {
columns: series_cols,
})
}
/// Creates an empty `DataFrame` usable in a compile time context (such as static initializers).
///
/// # Example
///
/// ```rust
/// use polars_core::prelude::DataFrame;
/// static EMPTY: DataFrame = DataFrame::empty();
/// ```
pub const fn empty() -> Self {
DataFrame::new_no_checks(Vec::new())
}
/// Removes the last `Series` from the `DataFrame` and returns it, or [`None`] if it is empty.
///
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let s1 = Series::new("Ocean", &["Atlantic", "Indian"]);
/// let s2 = Series::new("Area (km²)", &[106_460_000, 70_560_000]);
/// let mut df = DataFrame::new(vec![s1.clone(), s2.clone()])?;
///
/// assert_eq!(df.pop(), Some(s2));
/// assert_eq!(df.pop(), Some(s1));
/// assert_eq!(df.pop(), None);
/// assert!(df.is_empty());
/// # Ok::<(), PolarsError>(())
/// ```
pub fn pop(&mut self) -> Option<Series> {
self.columns.pop()
}
/// Add a new column at index 0 that counts the rows.
///
/// # Example
///
/// ```
/// # use polars_core::prelude::*;
/// let df1: DataFrame = df!("Name" => &["James", "Mary", "John", "Patricia"])?;
/// assert_eq!(df1.shape(), (4, 1));
///
/// let df2: DataFrame = df1.with_row_count("Id", None)?;
/// assert_eq!(df2.shape(), (4, 2));
/// println!("{}", df2);
///
/// # Ok::<(), PolarsError>(())
/// ```
///
/// Output:
///
/// ```text
/// shape: (4, 2)
/// +-----+----------+
/// | Id | Name |
/// | --- | --- |
/// | u32 | str |
/// +=====+==========+
/// | 0 | James |
/// +-----+----------+
/// | 1 | Mary |
/// +-----+----------+
/// | 2 | John |
/// +-----+----------+
/// | 3 | Patricia |
/// +-----+----------+
/// ```
pub fn with_row_count(&self, name: &str, offset: Option<IdxSize>) -> PolarsResult<Self> {
let mut columns = Vec::with_capacity(self.columns.len() + 1);
let offset = offset.unwrap_or(0);
let mut ca = IdxCa::from_vec(
name,
(offset..(self.height() as IdxSize) + offset).collect(),
);
ca.set_sorted(false);
columns.push(ca.into_series());
columns.extend_from_slice(&self.columns);
DataFrame::new(columns)
}
/// Add a row count in place.
pub fn with_row_count_mut(&mut self, name: &str, offset: Option<IdxSize>) -> &mut Self {
let offset = offset.unwrap_or(0);
let mut ca = IdxCa::from_vec(
name,
(offset..(self.height() as IdxSize) + offset).collect(),
);
ca.set_sorted(false);
self.columns.insert(0, ca.into_series());
self
}
/// Create a new `DataFrame` but does not check the length or duplicate occurrence of the `Series`.
///
/// It is advised to use [Series::new](Series::new) in favor of this method.
///
/// # Panic
/// It is the callers responsibility to uphold the contract of all `Series`
/// having an equal length, if not this may panic down the line.
pub const fn new_no_checks(columns: Vec<Series>) -> DataFrame {
DataFrame { columns }
}
/// Aggregate all chunks to contiguous memory.
#[must_use]
pub fn agg_chunks(&self) -> Self {
// Don't parallelize this. Memory overhead
let f = |s: &Series| s.rechunk();
let cols = self.columns.iter().map(f).collect();
DataFrame::new_no_checks(cols)
}
/// Shrink the capacity of this DataFrame to fit its length.
pub fn shrink_to_fit(&mut self) {
// Don't parallelize this. Memory overhead
for s in &mut self.columns {
s.shrink_to_fit();
}
}
/// Aggregate all the chunks in the DataFrame to a single chunk.
pub fn as_single_chunk(&mut self) -> &mut Self {
// Don't parallelize this. Memory overhead
for s in &mut self.columns {
*s = s.rechunk();
}
self
}
/// Aggregate all the chunks in the DataFrame to a single chunk in parallel.
/// This may lead to more peak memory consumption.
pub fn as_single_chunk_par(&mut self) -> &mut Self {
if self.columns.iter().any(|s| s.n_chunks() > 1) {
self.columns = self.apply_columns_par(&|s| s.rechunk());
}
self
}
/// Estimates of the DataFrames columns consist of the same chunk sizes
pub fn should_rechunk(&self) -> bool {
let hb = RandomState::default();
let hb2 = RandomState::with_seeds(392498, 98132457, 0, 412059);
!self
.columns
.iter()
// The idea is that we create a hash of the chunk lengths.
// Consisting of the combined hash + the sum (assuming collision probability is nihil)
// if not, we can add more hashes or at worst case we do an extra rechunk.
// the old solution to this was clone all lengths to a vec and compare the vecs
.map(|s| {
s.chunk_lengths().map(|i| i as u64).fold(
(0u64, 0u64, s.n_chunks()),
|(lhash, lh2, n), rval| {
let mut h = hb.build_hasher();
rval.hash(&mut h);
let rhash = h.finish();
let mut h = hb2.build_hasher();
rval.hash(&mut h);
let rh2 = h.finish();
(
_boost_hash_combine(lhash, rhash),
_boost_hash_combine(lh2, rh2),
n,
)
},
)
})
.all_equal()
}
/// Ensure all the chunks in the DataFrame are aligned.
pub fn rechunk(&mut self) -> &mut Self {
if self.should_rechunk() {
self.as_single_chunk_par()
} else {
self
}
}
/// Get the `DataFrame` schema.
///
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let df: DataFrame = df!("Thing" => &["Observable universe", "Human stupidity"],
/// "Diameter (m)" => &[8.8e26, f64::INFINITY])?;
///
/// let f1: Field = Field::new("Thing", DataType::Utf8);
/// let f2: Field = Field::new("Diameter (m)", DataType::Float64);
/// let sc: Schema = Schema::from(vec![f1, f2].into_iter());
///
/// assert_eq!(df.schema(), sc);
/// # Ok::<(), PolarsError>(())
/// ```
pub fn schema(&self) -> Schema {
Schema::from(self.iter().map(|s| s.field().into_owned()))
}
/// Get a reference to the `DataFrame` columns.
///
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let df: DataFrame = df!("Name" => &["Adenine", "Cytosine", "Guanine", "Thymine"],
/// "Symbol" => &["A", "C", "G", "T"])?;
/// let columns: &Vec<Series> = df.get_columns();
///
/// assert_eq!(columns[0].name(), "Name");
/// assert_eq!(columns[1].name(), "Symbol");
/// # Ok::<(), PolarsError>(())
/// ```
#[inline]
pub fn get_columns(&self) -> &Vec<Series> {
&self.columns
}
#[cfg(feature = "private")]
#[inline]
pub fn get_columns_mut(&mut self) -> &mut Vec<Series> {
&mut self.columns
}
/// Iterator over the columns as `Series`.
///
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let s1: Series = Series::new("Name", &["Pythagoras' theorem", "Shannon entropy"]);
/// let s2: Series = Series::new("Formula", &["a²+b²=c²", "H=-Σ[P(x)log|P(x)|]"]);
/// let df: DataFrame = DataFrame::new(vec![s1.clone(), s2.clone()])?;
///
/// let mut iterator = df.iter();
///
/// assert_eq!(iterator.next(), Some(&s1));
/// assert_eq!(iterator.next(), Some(&s2));
/// assert_eq!(iterator.next(), None);
/// # Ok::<(), PolarsError>(())
/// ```
pub fn iter(&self) -> std::slice::Iter<'_, Series> {
self.columns.iter()
}
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let df: DataFrame = df!("Language" => &["Rust", "Python"],
/// "Designer" => &["Graydon Hoare", "Guido van Rossum"])?;
///
/// assert_eq!(df.get_column_names(), &["Language", "Designer"]);
/// # Ok::<(), PolarsError>(())
/// ```
pub fn get_column_names(&self) -> Vec<&str> {
self.columns.iter().map(|s| s.name()).collect()
}
/// Get the `Vec<String>` representing the column names.
pub fn get_column_names_owned(&self) -> Vec<String> {
self.columns.iter().map(|s| s.name().to_string()).collect()
}
/// Set the column names.
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let mut df: DataFrame = df!("Mathematical set" => &["ℕ", "ℤ", "𝔻", "ℚ", "ℝ", "ℂ"])?;
/// df.set_column_names(&["Set"])?;
///
/// assert_eq!(df.get_column_names(), &["Set"]);
/// # Ok::<(), PolarsError>(())
/// ```
pub fn set_column_names<S: AsRef<str>>(&mut self, names: &[S]) -> PolarsResult<()> {
if names.len() != self.columns.len() {
return Err(PolarsError::ShapeMisMatch("the provided slice with column names has not the same size as the DataFrame's width".into()));
}
let unique_names: AHashSet<&str, ahash::RandomState> =
AHashSet::from_iter(names.iter().map(|name| name.as_ref()));
if unique_names.len() != self.columns.len() {
return Err(PolarsError::SchemaMisMatch(
"duplicate column names found".into(),
));
}
let columns = mem::take(&mut self.columns);
self.columns = columns
.into_iter()
.zip(names)
.map(|(s, name)| {
let mut s = s;
s.rename(name.as_ref());
s
})
.collect();
Ok(())
}
/// Get the data types of the columns in the DataFrame.
///
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let venus_air: DataFrame = df!("Element" => &["Carbon dioxide", "Nitrogen"],
/// "Fraction" => &[0.965, 0.035])?;
///
/// assert_eq!(venus_air.dtypes(), &[DataType::Utf8, DataType::Float64]);
/// # Ok::<(), PolarsError>(())
/// ```
pub fn dtypes(&self) -> Vec<DataType> {
self.columns.iter().map(|s| s.dtype().clone()).collect()
}
/// The number of chunks per column
pub fn n_chunks(&self) -> usize {
match self.columns.get(0) {
None => 0,
Some(s) => s.n_chunks(),
}
}
/// Get a reference to the schema fields of the `DataFrame`.
///
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let earth: DataFrame = df!("Surface type" => &["Water", "Land"],
/// "Fraction" => &[0.708, 0.292])?;
///
/// let f1: Field = Field::new("Surface type", DataType::Utf8);
/// let f2: Field = Field::new("Fraction", DataType::Float64);
///
/// assert_eq!(earth.fields(), &[f1, f2]);
/// # Ok::<(), PolarsError>(())
/// ```
pub fn fields(&self) -> Vec<Field> {
self.columns
.iter()
.map(|s| s.field().into_owned())
.collect()
}
/// Get (height, width) of the `DataFrame`.
///
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let df0: DataFrame = DataFrame::default();
/// let df1: DataFrame = df!("1" => &[1, 2, 3, 4, 5])?;
/// let df2: DataFrame = df!("1" => &[1, 2, 3, 4, 5],
/// "2" => &[1, 2, 3, 4, 5])?;
///
/// assert_eq!(df0.shape(), (0 ,0));
/// assert_eq!(df1.shape(), (5, 1));
/// assert_eq!(df2.shape(), (5, 2));
/// # Ok::<(), PolarsError>(())
/// ```
pub fn shape(&self) -> (usize, usize) {
match self.columns.as_slice() {
&[] => (0, 0),
v => (v[0].len(), v.len()),
}
}
/// Get the width of the `DataFrame` which is the number of columns.
///
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let df0: DataFrame = DataFrame::default();
/// let df1: DataFrame = df!("Series 1" => &[0; 0])?;
/// let df2: DataFrame = df!("Series 1" => &[0; 0],
/// "Series 2" => &[0; 0])?;
///
/// assert_eq!(df0.width(), 0);
/// assert_eq!(df1.width(), 1);
/// assert_eq!(df2.width(), 2);
/// # Ok::<(), PolarsError>(())
/// ```
pub fn width(&self) -> usize {
self.columns.len()
}
/// Get the height of the `DataFrame` which is the number of rows.
///
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let df0: DataFrame = DataFrame::default();
/// let df1: DataFrame = df!("Currency" => &["€", "$"])?;
/// let df2: DataFrame = df!("Currency" => &["€", "$", "¥", "£", "₿"])?;
///
/// assert_eq!(df0.height(), 0);
/// assert_eq!(df1.height(), 2);
/// assert_eq!(df2.height(), 5);
/// # Ok::<(), PolarsError>(())
/// ```
pub fn height(&self) -> usize {
self.shape().0
}
/// Check if the `DataFrame` is empty.
///
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let df1: DataFrame = DataFrame::default();
/// assert!(df1.is_empty());
///
/// let df2: DataFrame = df!("First name" => &["Forever"],
/// "Last name" => &["Alone"])?;
/// assert!(!df2.is_empty());
/// # Ok::<(), PolarsError>(())
/// ```
pub fn is_empty(&self) -> bool {
self.columns.is_empty()
}
pub(crate) fn hstack_mut_no_checks(&mut self, columns: &[Series]) -> &mut Self {
for col in columns {
self.columns.push(col.clone());
}
self
}
/// Add multiple `Series` to a `DataFrame`.
/// The added `Series` are required to have the same length.
///
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// fn stack(df: &mut DataFrame, columns: &[Series]) {
/// df.hstack_mut(columns);
/// }
/// ```
pub fn hstack_mut(&mut self, columns: &[Series]) -> PolarsResult<&mut Self> {
let mut names = PlHashSet::with_capacity(self.columns.len());
for s in &self.columns {
names.insert(s.name());
}
let height = self.height();
// first loop check validity. We don't do this in a single pass otherwise
// this DataFrame is already modified when an error occurs.
for col in columns {
if col.len() != height && height != 0 {
return Err(PolarsError::ShapeMisMatch(
format!("Could not horizontally stack Series. The Series length {} differs from the DataFrame height: {height}", col.len()).into()));
}
let name = col.name();
if names.contains(name) {
return Err(PolarsError::Duplicate(
format!("Cannot do hstack operation. Column with name: {name} already exists",)
.into(),
));
}
names.insert(name);
}
drop(names);
Ok(self.hstack_mut_no_checks(columns))
}
/// Add multiple `Series` to a `DataFrame`.
/// The added `Series` are required to have the same length.
///
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let df1: DataFrame = df!("Element" => &["Copper", "Silver", "Gold"])?;
/// let s1: Series = Series::new("Proton", &[29, 47, 79]);
/// let s2: Series = Series::new("Electron", &[29, 47, 79]);
///
/// let df2: DataFrame = df1.hstack(&[s1, s2])?;
/// assert_eq!(df2.shape(), (3, 3));
/// println!("{}", df2);
/// # Ok::<(), PolarsError>(())
/// ```
///
/// Output:
///
/// ```text
/// shape: (3, 3)
/// +---------+--------+----------+
/// | Element | Proton | Electron |
/// | --- | --- | --- |
/// | str | i32 | i32 |
/// +=========+========+==========+
/// | Copper | 29 | 29 |
/// +---------+--------+----------+
/// | Silver | 47 | 47 |
/// +---------+--------+----------+
/// | Gold | 79 | 79 |
/// +---------+--------+----------+
/// ```
pub fn hstack(&self, columns: &[Series]) -> PolarsResult<Self> {
let mut new_cols = self.columns.clone();
new_cols.extend_from_slice(columns);
DataFrame::new(new_cols)
}
/// Concatenate a `DataFrame` to this `DataFrame` and return as newly allocated `DataFrame`.
///
/// If many `vstack` operations are done, it is recommended to call [`DataFrame::rechunk`].
///
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let df1: DataFrame = df!("Element" => &["Copper", "Silver", "Gold"],
/// "Melting Point (K)" => &[1357.77, 1234.93, 1337.33])?;
/// let df2: DataFrame = df!("Element" => &["Platinum", "Palladium"],
/// "Melting Point (K)" => &[2041.4, 1828.05])?;
///
/// let df3: DataFrame = df1.vstack(&df2)?;
///
/// assert_eq!(df3.shape(), (5, 2));
/// println!("{}", df3);
/// # Ok::<(), PolarsError>(())
/// ```
///
/// Output:
///
/// ```text
/// shape: (5, 2)
/// +-----------+-------------------+
/// | Element | Melting Point (K) |
/// | --- | --- |
/// | str | f64 |
/// +===========+===================+
/// | Copper | 1357.77 |
/// +-----------+-------------------+
/// | Silver | 1234.93 |
/// +-----------+-------------------+
/// | Gold | 1337.33 |
/// +-----------+-------------------+
/// | Platinum | 2041.4 |
/// +-----------+-------------------+
/// | Palladium | 1828.05 |
/// +-----------+-------------------+
/// ```
pub fn vstack(&self, other: &DataFrame) -> PolarsResult<Self> {
let mut df = self.clone();
df.vstack_mut(other)?;
Ok(df)
}
/// Concatenate a DataFrame to this DataFrame
///
/// If many `vstack` operations are done, it is recommended to call [`DataFrame::rechunk`].
///
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let mut df1: DataFrame = df!("Element" => &["Copper", "Silver", "Gold"],
/// "Melting Point (K)" => &[1357.77, 1234.93, 1337.33])?;
/// let df2: DataFrame = df!("Element" => &["Platinum", "Palladium"],
/// "Melting Point (K)" => &[2041.4, 1828.05])?;
///
/// df1.vstack_mut(&df2)?;
///
/// assert_eq!(df1.shape(), (5, 2));
/// println!("{}", df1);
/// # Ok::<(), PolarsError>(())
/// ```
///
/// Output:
///
/// ```text
/// shape: (5, 2)
/// +-----------+-------------------+
/// | Element | Melting Point (K) |
/// | --- | --- |
/// | str | f64 |
/// +===========+===================+
/// | Copper | 1357.77 |
/// +-----------+-------------------+
/// | Silver | 1234.93 |
/// +-----------+-------------------+
/// | Gold | 1337.33 |
/// +-----------+-------------------+
/// | Platinum | 2041.4 |
/// +-----------+-------------------+
/// | Palladium | 1828.05 |
/// +-----------+-------------------+
/// ```
pub fn vstack_mut(&mut self, other: &DataFrame) -> PolarsResult<&mut Self> {
if self.width() != other.width() {
if self.width() == 0 {
self.columns = other.columns.clone();
return Ok(self);
}
return Err(PolarsError::ShapeMisMatch(
format!("Could not vertically stack DataFrame. The DataFrames appended width {} differs from the parent DataFrames width {}", self.width(), other.width()).into()
));
}
self.columns
.iter_mut()
.zip(other.columns.iter())
.try_for_each::<_, PolarsResult<_>>(|(left, right)| {
can_extend(left, right)?;
left.append(right).expect("should not fail");
Ok(())
})?;
Ok(self)
}
/// Does not check if schema is correct
pub(crate) fn vstack_mut_unchecked(&mut self, other: &DataFrame) {
self.columns
.iter_mut()
.zip(other.columns.iter())
.for_each(|(left, right)| {
left.append(right).expect("should not fail");
});
}
/// Extend the memory backed by this [`DataFrame`] with the values from `other`.
///
/// Different from [`vstack`](Self::vstack) which adds the chunks from `other` to the chunks of this [`DataFrame`]
/// `extend` appends the data from `other` to the underlying memory locations and thus may cause a reallocation.
///
/// If this does not cause a reallocation, the resulting data structure will not have any extra chunks
/// and thus will yield faster queries.
///
/// Prefer `extend` over `vstack` when you want to do a query after a single append. For instance during
/// online operations where you add `n` rows and rerun a query.
///
/// Prefer `vstack` over `extend` when you want to append many times before doing a query. For instance
/// when you read in multiple files and when to store them in a single `DataFrame`. In the latter case, finish the sequence
/// of `append` operations with a [`rechunk`](Self::rechunk).
pub fn extend(&mut self, other: &DataFrame) -> PolarsResult<()> {
if self.width() != other.width() {
return Err(PolarsError::ShapeMisMatch(
format!("Could not extend DataFrame. The DataFrames extended width {} differs from the parent DataFrames width {}", self.width(), other.width()).into()
));
}
self.columns
.iter_mut()
.zip(other.columns.iter())
.try_for_each::<_, PolarsResult<_>>(|(left, right)| {
can_extend(left, right)?;
left.extend(right).unwrap();
Ok(())
})?;
Ok(())
}
/// Remove a column by name and return the column removed.
///
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let mut df: DataFrame = df!("Animal" => &["Tiger", "Lion", "Great auk"],
/// "IUCN" => &["Endangered", "Vulnerable", "Extinct"])?;
///
/// let s1: PolarsResult<Series> = df.drop_in_place("Average weight");
/// assert!(s1.is_err());
///
/// let s2: Series = df.drop_in_place("Animal")?;
/// assert_eq!(s2, Series::new("Animal", &["Tiger", "Lion", "Great auk"]));
/// # Ok::<(), PolarsError>(())
/// ```
pub fn drop_in_place(&mut self, name: &str) -> PolarsResult<Series> {
let idx = self.check_name_to_idx(name)?;
Ok(self.columns.remove(idx))
}
/// Return a new `DataFrame` where all null values are dropped.
///
/// # Example
///
/// ```no_run
/// # use polars_core::prelude::*;
/// let df1: DataFrame = df!("Country" => ["Malta", "Liechtenstein", "North Korea"],
/// "Tax revenue (% GDP)" => [Some(32.7), None, None])?;
/// assert_eq!(df1.shape(), (3, 2));
///
/// let df2: DataFrame = df1.drop_nulls(None)?;
/// assert_eq!(df2.shape(), (1, 2));
/// println!("{}", df2);
/// # Ok::<(), PolarsError>(())
/// ```
///
/// Output:
///
/// ```text
/// shape: (1, 2)
/// +---------+---------------------+
/// | Country | Tax revenue (% GDP) |
/// | --- | --- |
/// | str | f64 |
/// +=========+=====================+
/// | Malta | 32.7 |
/// +---------+---------------------+
/// ```
pub fn drop_nulls(&self, subset: Option<&[String]>) -> PolarsResult<Self> {
let selected_series;
let mut iter = match subset {
Some(cols) => {
selected_series = self.select_series(cols)?;
selected_series.iter()
}
None => self.columns.iter(),
};
// fast path for no nulls in df
if iter.clone().all(|s| !s.has_validity()) {
return Ok(self.clone());
}
let mask = iter
.next()
.ok_or_else(|| PolarsError::NoData("No data to drop nulls from".into()))?;
let mut mask = mask.is_not_null();
for s in iter {
mask = mask & s.is_not_null();
}
self.filter(&mask)
}
/// Drop a column by name.
/// This is a pure method and will return a new `DataFrame` instead of modifying
/// the current one in place.
///
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let df1: DataFrame = df!("Ray type" => &["α", "β", "X", "γ"])?;
/// let df2: DataFrame = df1.drop("Ray type")?;
///
/// assert!(df2.is_empty());
/// # Ok::<(), PolarsError>(())
/// ```
pub fn drop(&self, name: &str) -> PolarsResult<Self> {
let idx = self.check_name_to_idx(name)?;
let mut new_cols = Vec::with_capacity(self.columns.len() - 1);
self.columns.iter().enumerate().for_each(|(i, s)| {
if i != idx {
new_cols.push(s.clone())
}
});
Ok(DataFrame::new_no_checks(new_cols))
}
pub fn drop_many<S: AsRef<str>>(&self, names: &[S]) -> Self {
let names = names.iter().map(|s| s.as_ref()).collect();
fn inner(df: &DataFrame, names: Vec<&str>) -> DataFrame {
let mut new_cols = Vec::with_capacity(df.columns.len() - names.len());
df.columns.iter().for_each(|s| {
if !names.contains(&s.name()) {
new_cols.push(s.clone())
}
});
DataFrame::new_no_checks(new_cols)
}
inner(self, names)
}
fn insert_at_idx_no_name_check(
&mut self,
index: usize,
series: Series,
) -> PolarsResult<&mut Self> {
if series.len() == self.height() {
self.columns.insert(index, series);
Ok(self)
} else {
Err(PolarsError::ShapeMisMatch(
format!(
"Could not add column. The Series length {} differs from the DataFrame height: {}",
series.len(),
self.height()
)
.into(),
))
}
}
/// Insert a new column at a given index.
pub fn insert_at_idx<S: IntoSeries>(
&mut self,
index: usize,
column: S,
) -> PolarsResult<&mut Self> {
let series = column.into_series();
self.check_already_present(series.name())?;
self.insert_at_idx_no_name_check(index, series)
}
fn add_column_by_search(&mut self, series: Series) -> PolarsResult<()> {
if let Some(idx) = self.find_idx_by_name(series.name()) {
self.replace_at_idx(idx, series)?;
} else {
self.columns.push(series);
}
Ok(())
}
/// Add a new column to this `DataFrame` or replace an existing one.
pub fn with_column<S: IntoSeries>(&mut self, column: S) -> PolarsResult<&mut Self> {
fn inner(df: &mut DataFrame, mut series: Series) -> PolarsResult<&mut DataFrame> {
let height = df.height();
if series.len() == 1 && height > 1 {
series = series.new_from_index(0, height);
}
if series.len() == height || df.is_empty() {
df.add_column_by_search(series)?;
Ok(df)
}
// special case for literals
else if height == 0 && series.len() == 1 {
let s = series.slice(0, 0);
df.add_column_by_search(s)?;
Ok(df)
} else {
Err(PolarsError::ShapeMisMatch(
format!(
"Could not add column. The Series length {} differs from the DataFrame height: {}",
series.len(),
df.height()
)
.into(),
))
}
}
let series = column.into_series();
inner(self, series)
}
fn add_column_by_schema(&mut self, s: Series, schema: &Schema) -> PolarsResult<()> {
let name = s.name();
if let Some((idx, _, _)) = schema.get_full(name) {
// schema is incorrect fallback to search
if self.columns.get(idx).map(|s| s.name()) != Some(name) {
self.add_column_by_search(s)?;
} else {
self.replace_at_idx(idx, s)?;
}
} else {
self.columns.push(s);
}
Ok(())
}
pub fn _add_columns(&mut self, columns: Vec<Series>, schema: &Schema) -> PolarsResult<()> {
for (i, s) in columns.into_iter().enumerate() {
// we need to branch here
// because users can add multiple columns with the same name
if i == 0 || schema.get(s.name()).is_some() {
self.with_column_and_schema(s, schema)?;
} else {
self.with_column(s.clone())?;
}
}
Ok(())
}
/// Add a new column to this `DataFrame` or replace an existing one.
/// Uses an existing schema to amortize lookups.
/// If the schema is incorrect, we will fallback to linear search.
pub fn with_column_and_schema<S: IntoSeries>(
&mut self,
column: S,
schema: &Schema,
) -> PolarsResult<&mut Self> {
let mut series = column.into_series();
let height = self.height();
if series.len() == 1 && height > 1 {
series = series.new_from_index(0, height);
}
if series.len() == height || self.is_empty() {
self.add_column_by_schema(series, schema)?;
Ok(self)
}
// special case for literals
else if height == 0 && series.len() == 1 {
let s = series.slice(0, 0);
self.add_column_by_schema(s, schema)?;
Ok(self)
} else {
Err(PolarsError::ShapeMisMatch(
format!(
"Could not add column. The Series length {} differs from the DataFrame height: {}",
series.len(),
self.height()
)
.into(),
))
}
}
/// Get a row in the `DataFrame`. Beware this is slow.
///
/// # Example
///
/// ```
/// # use polars_core::prelude::*;
/// fn example(df: &mut DataFrame, idx: usize) -> Option<Vec<AnyValue>> {
/// df.get(idx)
/// }
/// ```
pub fn get(&self, idx: usize) -> Option<Vec<AnyValue>> {
match self.columns.get(0) {
Some(s) => {
if s.len() <= idx {
return None;
}
}
None => return None,
}
// safety: we just checked bounds
unsafe { Some(self.columns.iter().map(|s| s.get_unchecked(idx)).collect()) }
}
/// Select a `Series` by index.
///
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let df: DataFrame = df!("Star" => &["Sun", "Betelgeuse", "Sirius A", "Sirius B"],
/// "Absolute magnitude" => &[4.83, -5.85, 1.42, 11.18])?;
///
/// let s1: Option<&Series> = df.select_at_idx(0);
/// let s2: Series = Series::new("Star", &["Sun", "Betelgeuse", "Sirius A", "Sirius B"]);
///
/// assert_eq!(s1, Some(&s2));
/// # Ok::<(), PolarsError>(())
/// ```
pub fn select_at_idx(&self, idx: usize) -> Option<&Series> {
self.columns.get(idx)
}
/// Select a mutable series by index.
///
/// *Note: the length of the Series should remain the same otherwise the DataFrame is invalid.*
/// For this reason the method is not public
fn select_at_idx_mut(&mut self, idx: usize) -> Option<&mut Series> {
self.columns.get_mut(idx)
}
/// Select column(s) from this `DataFrame` by range and return a new DataFrame
///
/// # Examples
///
/// ```rust
/// # use polars_core::prelude::*;
/// let df = df! {
/// "0" => &[0, 0, 0],
/// "1" => &[1, 1, 1],
/// "2" => &[2, 2, 2]
/// }?;
///
/// assert!(df.select(&["0", "1"])?.frame_equal(&df.select_by_range(0..=1)?));
/// assert!(df.frame_equal(&df.select_by_range(..)?));
/// # Ok::<(), PolarsError>(())
/// ```
pub fn select_by_range<R>(&self, range: R) -> PolarsResult<Self>
where
R: ops::RangeBounds<usize>,
{
// This function is copied from std::slice::range (https://doc.rust-lang.org/std/slice/fn.range.html)
// because it is the nightly feature. We should change here if this function were stable.
fn get_range<R>(range: R, bounds: ops::RangeTo<usize>) -> ops::Range<usize>
where
R: ops::RangeBounds<usize>,
{
let len = bounds.end;
let start: ops::Bound<&usize> = range.start_bound();
let start = match start {
ops::Bound::Included(&start) => start,
ops::Bound::Excluded(start) => start.checked_add(1).unwrap_or_else(|| {
panic!("attempted to index slice from after maximum usize");
}),
ops::Bound::Unbounded => 0,
};
let end: ops::Bound<&usize> = range.end_bound();
let end = match end {
ops::Bound::Included(end) => end.checked_add(1).unwrap_or_else(|| {
panic!("attempted to index slice up to maximum usize");
}),
ops::Bound::Excluded(&end) => end,
ops::Bound::Unbounded => len,
};
if start > end {
panic!("slice index starts at {start} but ends at {end}");
}
if end > len {
panic!("range end index {end} out of range for slice of length {len}",);
}
ops::Range { start, end }
}
let colnames = self.get_column_names_owned();
let range = get_range(range, ..colnames.len());
self.select_impl(&colnames[range])
}
/// Get column index of a `Series` by name.
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let df: DataFrame = df!("Name" => &["Player 1", "Player 2", "Player 3"],
/// "Health" => &[100, 200, 500],
/// "Mana" => &[250, 100, 0],
/// "Strength" => &[30, 150, 300])?;
///
/// assert_eq!(df.find_idx_by_name("Name"), Some(0));
/// assert_eq!(df.find_idx_by_name("Health"), Some(1));
/// assert_eq!(df.find_idx_by_name("Mana"), Some(2));
/// assert_eq!(df.find_idx_by_name("Strength"), Some(3));
/// assert_eq!(df.find_idx_by_name("Haste"), None);
/// # Ok::<(), PolarsError>(())
/// ```
pub fn find_idx_by_name(&self, name: &str) -> Option<usize> {
self.columns.iter().position(|s| s.name() == name)
}
/// Select a single column by name.
///
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let s1: Series = Series::new("Password", &["123456", "[]B$u$g$s$B#u#n#n#y[]{}"]);
/// let s2: Series = Series::new("Robustness", &["Weak", "Strong"]);
/// let df: DataFrame = DataFrame::new(vec![s1.clone(), s2])?;
///
/// assert_eq!(df.column("Password")?, &s1);
/// # Ok::<(), PolarsError>(())
/// ```
pub fn column(&self, name: &str) -> PolarsResult<&Series> {
let idx = self
.find_idx_by_name(name)
.ok_or_else(|| PolarsError::NotFound(name.to_string().into()))?;
Ok(self.select_at_idx(idx).unwrap())
}
/// Selected multiple columns by name.
///
/// # Example
///
/// ```rust
/// # use polars_core::prelude::*;
/// let df: DataFrame = df!("Latin name" => &["Oncorhynchus kisutch", "Salmo salar"],
/// "Max weight (kg)" => &[16.0, 35.89])?;
/// let sv: Vec<&Series> = df.columns(&["Latin name", "Max weight (kg)"])?;
///
/// assert_eq!(&df[0], sv[0]);
/// assert_eq!(&df[1], sv[1]);
/// # Ok::<(), PolarsError>(())
/// ```
pub fn columns<I, S>(&self, names: I) -> PolarsResult<Vec<&Series>>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
names
.into_iter()
.map(|name| self.column(name.as_ref()))
.collect()
}
/// Select column(s) from this `DataFrame` and return a new `DataFrame`.
///
/// # Examples
///
/// ```
/// # use polars_core::prelude::*;
/// fn example(df: &DataFrame) -> PolarsResult<DataFrame> {
/// df.select(["foo", "bar"])
/// }
/// ```
pub fn select<I, S>(&self, selection: I) -> PolarsResult<Self>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let cols = selection
.into_iter()
.map(|s| s.as_ref().to_string())
.collect::<Vec<_>>();
self.select_impl(&cols)
}
fn select_impl(&self, cols: &[String]) -> PolarsResult<Self> {
self.select_check_duplicates(cols)?;
let selected = self.select_series_impl(cols)?;
Ok(DataFrame::new_no_checks(selected))
}
pub fn select_physical<I, S>(&self, selection: I) -> PolarsResult<Self>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let cols = selection
.into_iter()
.map(|s| s.as_ref().to_string())
.collect::<Vec<_>>();
self.select_physical_impl(&cols)
}
fn select_physical_impl(&self, cols: &[String]) -> PolarsResult<Self> {
self.select_check_duplicates(cols)?;
let selected = self.select_series_physical_impl(cols)?;
Ok(DataFrame::new_no_checks(selected))
}
fn select_check_duplicates(&self, cols: &[String]) -> PolarsResult<()> {
let mut names = PlHashSet::with_capacity(cols.len());
for name in cols {
if !names.insert(name.as_str()) {
_duplicate_err(name)?
}
}
Ok(())
}