Skip to content

Commit

Permalink
chore[python]: feature flag features (#4511)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Aug 21, 2022
1 parent 2a40366 commit 5eb0837
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 24 deletions.
23 changes: 14 additions & 9 deletions py-polars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ pct_change = ["polars/pct_change"]
repeat_by = ["polars/repeat_by"]
simd = ["polars/simd"]
meta = ["polars/meta"]
search_sorted = ["polars/search_sorted"]
# Use "decompress" instead of "decompress-fast" till zlib-ng is fixed.
decompress = ["polars/decompress"]
lazy_regex = ["polars/lazy_regex"]
csv-file = ["polars/csv-file"]
object = ["polars/object"]
extract_jsonpath = ["polars/extract_jsonpath"]

all = [
"json",
Expand All @@ -63,8 +70,14 @@ all = [
"asof_join",
"cross_join",
"pct_change",
"polars/search_sorted",
"search_sorted",
"meta",
"decompress",
"lazy_regex",
"csv-file",
"extract_jsonpath",
"polars/timezones",
"object",
]

# we cannot conditionaly activate simd
Expand All @@ -86,8 +99,6 @@ features = [
"strings",
"temporal",
"random",
"object",
"csv-file",
"fmt",
"performant",
"dtype-full",
Expand All @@ -99,11 +110,7 @@ features = [
"concat_str",
"row_hash",
"reinterpret",
# Use "decompress" instead of "decompress-fast" till zlib-ng is fixed.
"decompress",
"mode",
"extract_jsonpath",
"lazy_regex",
"cum_agg",
"rolling_window",
"interpolate",
Expand All @@ -120,7 +127,6 @@ features = [
"ewma",
"dot_diagram",
"dataframe_arithmetic",
"json",
"string_encoding",
"product",
"ndarray",
Expand All @@ -135,7 +141,6 @@ features = [
"to_dummies",
"string_justify",
"arg_where",
"timezones",
"date_offset",
]

Expand Down
1 change: 1 addition & 0 deletions py-polars/src/apply/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ fn iterator_to_bool(
ca
}

#[cfg(feature = "object")]
fn iterator_to_object(
it: impl Iterator<Item = Option<ObjectValue>>,
init_null_count: usize,
Expand Down
42 changes: 32 additions & 10 deletions py-polars/src/apply/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,21 @@ fn infer_and_finish<'a, A: ApplyLambda<'a>>(
.apply_extract_any_values(py, lambda, null_count, av.0)
.map(|s| s.into())
} else {
applyer
.apply_lambda_with_object_out_type(
py,
lambda,
null_count,
Some(out.to_object(py).into()),
)
.map(|ca| ca.into_series().into())
#[cfg(feature = "object")]
{
applyer
.apply_lambda_with_object_out_type(
py,
lambda,
null_count,
Some(out.to_object(py).into()),
)
.map(|ca| ca.into_series().into())
}
#[cfg(not(feature = "object"))]
{
todo!()
}
}
}

Expand Down Expand Up @@ -183,6 +190,7 @@ pub trait ApplyLambda<'a> {
) -> PyResult<Series>;

/// Apply a lambda with list output type
#[cfg(feature = "object")]
fn apply_lambda_with_object_out_type(
&'a self,
py: Python,
Expand Down Expand Up @@ -474,6 +482,7 @@ impl<'a> ApplyLambda<'a> for BooleanChunked {
Ok(Series::new(self.name(), &avs))
}

#[cfg(feature = "object")]
fn apply_lambda_with_object_out_type(
&'a self,
py: Python,
Expand Down Expand Up @@ -769,6 +778,7 @@ where
Ok(Series::new(self.name(), &avs))
}

#[cfg(feature = "object")]
fn apply_lambda_with_object_out_type(
&'a self,
py: Python,
Expand Down Expand Up @@ -1058,6 +1068,7 @@ impl<'a> ApplyLambda<'a> for Utf8Chunked {
Ok(Series::new(self.name(), &avs))
}

#[cfg(feature = "object")]
fn apply_lambda_with_object_out_type(
&'a self,
py: Python,
Expand Down Expand Up @@ -1571,6 +1582,7 @@ impl<'a> ApplyLambda<'a> for ListChunked {
Ok(Series::new(self.name(), &avs))
}

#[cfg(feature = "object")]
fn apply_lambda_with_object_out_type(
&'a self,
py: Python,
Expand Down Expand Up @@ -1633,6 +1645,7 @@ impl<'a> ApplyLambda<'a> for ListChunked {
}
}

#[cfg(feature = "object")]
impl<'a> ApplyLambda<'a> for ObjectChunked<ObjectValue> {
fn apply_lambda_unknown(&'a self, py: Python, lambda: &'a PyAny) -> PyResult<PySeries> {
let mut null_count = 0;
Expand All @@ -1655,8 +1668,15 @@ impl<'a> ApplyLambda<'a> for ObjectChunked<ObjectValue> {
}

fn apply_lambda(&'a self, py: Python, lambda: &'a PyAny) -> PyResult<PySeries> {
self.apply_lambda_with_object_out_type(py, lambda, 0, None)
.map(|ca| PySeries::new(ca.into_series()))
#[cfg(feature = "object")]
{
self.apply_lambda_with_object_out_type(py, lambda, 0, None)
.map(|ca| PySeries::new(ca.into_series()))
}
#[cfg(not(feature = "object"))]
{
todo!()
}
}

fn apply_to_struct(
Expand Down Expand Up @@ -1869,6 +1889,7 @@ impl<'a> ApplyLambda<'a> for ObjectChunked<ObjectValue> {
Ok(Series::new(self.name(), &avs))
}

#[cfg(feature = "object")]
fn apply_lambda_with_object_out_type(
&'a self,
py: Python,
Expand Down Expand Up @@ -2083,6 +2104,7 @@ impl<'a> ApplyLambda<'a> for StructChunked {
Ok(Series::new(self.name(), &avs))
}

#[cfg(feature = "object")]
fn apply_lambda_with_object_out_type(
&'a self,
py: Python,
Expand Down
6 changes: 6 additions & 0 deletions py-polars/src/conversion.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};

#[cfg(feature = "object")]
use polars::chunked_array::object::PolarsObjectSafe;
use polars::frame::row::Row;
use polars::frame::{groupby::PivotAgg, NullStrategy};
Expand Down Expand Up @@ -217,6 +218,7 @@ impl IntoPy<PyObject> for Wrap<AnyValue<'_>> {
AnyValue::List(v) => PySeries::new(v).to_list(),
AnyValue::Struct(vals, flds) => struct_dict(py, vals, flds),
AnyValue::StructOwned(payload) => struct_dict(py, payload.0, &payload.1),
#[cfg(feature = "object")]
AnyValue::Object(v) => {
let s = format!("{}", v);
s.into_py(py)
Expand Down Expand Up @@ -259,6 +261,7 @@ impl ToPyObject for Wrap<DataType> {
let duration_class = pl.getattr("Duration").unwrap();
duration_class.call1((tu.to_ascii(),)).unwrap().into()
}
#[cfg(feature = "object")]
DataType::Object(_) => pl.getattr("Object").unwrap().into(),
DataType::Categorical(_) => pl.getattr("Categorical").unwrap().into(),
DataType::Time => pl.getattr("Time").unwrap().into(),
Expand Down Expand Up @@ -313,6 +316,7 @@ impl FromPyObject<'_> for Wrap<DataType> {
"Duration" => DataType::Duration(TimeUnit::Microseconds),
"Float32" => DataType::Float32,
"Float64" => DataType::Float64,
#[cfg(feature = "object")]
"Object" => DataType::Object("unknown"),
"List" => DataType::List(Box::new(DataType::Boolean)),
"Null" => DataType::Null,
Expand Down Expand Up @@ -631,6 +635,7 @@ impl Display for ObjectValue {
}
}

#[cfg(feature = "object")]
impl PolarsObject for ObjectValue {
fn type_name() -> &'static str {
"object"
Expand All @@ -656,6 +661,7 @@ impl<'a> FromPyObject<'a> for ObjectValue {
/// # Safety
///
/// The caller is responsible for checking that val is Object otherwise UB
#[cfg(feature = "object")]
impl From<&dyn PolarsObjectSafe> for &ObjectValue {
fn from(val: &dyn PolarsObjectSafe) -> Self {
unsafe { &*(val as *const dyn PolarsObjectSafe as *const ObjectValue) }
Expand Down
3 changes: 3 additions & 0 deletions py-polars/src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl PyDataFrame {

#[staticmethod]
#[allow(clippy::too_many_arguments)]
#[cfg(feature = "csv-file")]
pub fn read_csv(
py_f: &PyAny,
infer_schema_length: Option<usize>,
Expand Down Expand Up @@ -488,6 +489,7 @@ impl PyDataFrame {
Ok(())
}

#[cfg(feature = "object")]
pub fn row_tuple(&self, idx: i64) -> PyObject {
Python::with_gil(|py| {
let idx = if idx < 0 {
Expand All @@ -509,6 +511,7 @@ impl PyDataFrame {
})
}

#[cfg(feature = "object")]
pub fn row_tuples(&self) -> PyObject {
Python::with_gil(|py| {
let df = &self.df;
Expand Down
3 changes: 3 additions & 0 deletions py-polars/src/datatypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub(crate) enum PyDataType {
Datetime(TimeUnit, Option<TimeZone>),
Duration(TimeUnit),
Time,
#[cfg(feature = "object")]
Object,
Categorical,
Struct,
Expand All @@ -49,6 +50,7 @@ impl From<&DataType> for PyDataType {
DataType::Datetime(tu, tz) => Datetime(*tu, tz.clone()),
DataType::Duration(tu) => Duration(*tu),
DataType::Time => Time,
#[cfg(feature = "object")]
DataType::Object(_) => Object,
DataType::Categorical(_) => Categorical,
DataType::Struct(_) => Struct,
Expand Down Expand Up @@ -86,6 +88,7 @@ impl From<PyDataType> for DataType {
PyDataType::Datetime(tu, tz) => Datetime(tu, tz),
PyDataType::Duration(tu) => Duration(tu),
PyDataType::Time => Time,
#[cfg(feature = "object")]
PyDataType::Object => Object("object"),
PyDataType::Categorical => Categorical(None),
PyDataType::Struct => Struct(vec![]),
Expand Down
9 changes: 6 additions & 3 deletions py-polars/src/lazy/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ use std::borrow::Cow;
use std::io::BufWriter;

use polars::io::RowCount;
use polars::lazy::frame::{
AllowedOptimizations, LazyCsvReader, LazyFrame, LazyGroupBy, LazyJsonLineReader,
};
#[cfg(feature = "csv-file")]
use polars::lazy::frame::LazyCsvReader;
#[cfg(feature = "json")]
use polars::lazy::frame::LazyJsonLineReader;
use polars::lazy::frame::{AllowedOptimizations, LazyFrame, LazyGroupBy};
use polars::lazy::prelude::col;
use polars::prelude::{ClosedWindow, CsvEncoding, DataFrame, Field, JoinType, Schema};
use polars::time::*;
Expand Down Expand Up @@ -166,6 +168,7 @@ impl PyLazyFrame {

#[staticmethod]
#[allow(clippy::too_many_arguments)]
#[cfg(feature = "csv-file")]
pub fn new_from_csv(
path: String,
sep: &str,
Expand Down
3 changes: 3 additions & 0 deletions py-polars/src/lazy/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ impl PyExpr {
self.clone().inner.arg_min().into()
}

#[cfg(feature = "search_sorted")]
pub fn search_sorted(&self, element: PyExpr) -> PyExpr {
self.inner.clone().search_sorted(element.inner).into()
}
Expand Down Expand Up @@ -706,6 +707,7 @@ impl PyExpr {
.with_fmt("str.base64_decode")
.into()
}
#[cfg(feature = "extract_jsonpath")]
pub fn str_json_path_match(&self, pat: String) -> PyExpr {
let function = move |s: Series| {
let ca = s.utf8()?;
Expand Down Expand Up @@ -754,6 +756,7 @@ impl PyExpr {
self.inner.clone().arr().lengths().into()
}

#[cfg(feature = "is_in")]
pub fn arr_contains(&self, other: PyExpr) -> PyExpr {
self.inner.clone().arr().contains(other.inner).into()
}
Expand Down
1 change: 1 addition & 0 deletions py-polars/src/lazy/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod apply;
pub mod dataframe;
pub mod dsl;
#[cfg(feature = "meta")]
mod meta;
pub mod utils;
pub use apply::*;
Expand Down
15 changes: 13 additions & 2 deletions py-polars/src/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,15 @@ impl PySeries {

#[staticmethod]
pub fn new_object(name: &str, val: Vec<ObjectValue>, _strict: bool) -> Self {
let s = ObjectChunked::<ObjectValue>::new_from_vec(name, val).into_series();
s.into()
#[cfg(feature = "object")]
{
let s = ObjectChunked::<ObjectValue>::new_from_vec(name, val).into_series();
s.into()
}
#[cfg(not(feature = "object"))]
{
todo!()
}
}

#[staticmethod]
Expand Down Expand Up @@ -348,6 +355,7 @@ impl PySeries {
self.series.estimated_size()
}

#[cfg(feature = "object")]
pub fn get_object(&self, index: usize) -> PyObject {
Python::with_gil(|py| {
if matches!(self.series.dtype(), DataType::Object(_)) {
Expand Down Expand Up @@ -776,6 +784,7 @@ impl PySeries {
DataType::Categorical(_) => {
PyList::new(py, series.categorical().unwrap().iter_str())
}
#[cfg(feature = "object")]
DataType::Object(_) => {
let v = PyList::empty(py);
for i in 0..series.len() {
Expand Down Expand Up @@ -1082,6 +1091,7 @@ impl PySeries {
)?;
ca.into_series()
}
#[cfg(feature = "object")]
Some(DataType::Object(_)) => {
let ca: ObjectChunked<ObjectValue> = apply_method_all_arrow_series2!(
series,
Expand Down Expand Up @@ -1140,6 +1150,7 @@ impl PySeries {
Ok(s.into())
}

#[cfg(feature = "extract_jsonpath")]
pub fn str_json_path_match(&self, pat: &str) -> PyResult<Self> {
let ca = self.series.utf8().map_err(PyPolarsErr::from)?;
let s = ca
Expand Down

0 comments on commit 5eb0837

Please sign in to comment.