diff --git a/examples/json_path/src/main.rs b/examples/json_path/src/main.rs index 246b4b5f90fd5..dae2accc0fcd8 100644 --- a/examples/json_path/src/main.rs +++ b/examples/json_path/src/main.rs @@ -7,7 +7,7 @@ fn main() -> Result<()> { Some(r#"{"a": 1, "b": [{"c": 0}, {"c": 1}]}"#), Some(r#"{"a": 2, "b": [{"c": 2}, {"c": 5}]}"#), None, - ] + ], ); let ca = s.utf8()?; diff --git a/polars/polars-core/src/chunked_array/strings/json_path.rs b/polars/polars-core/src/chunked_array/strings/json_path.rs index f112934797281..fd337cb82b341 100644 --- a/polars/polars-core/src/chunked_array/strings/json_path.rs +++ b/polars/polars-core/src/chunked_array/strings/json_path.rs @@ -1,9 +1,8 @@ use crate::prelude::*; +use arrow::io::ndjson; use jsonpath_lib::PathCompiled; use serde_json::Value; use std::borrow::Cow; -use arrow::io::ndjson; - #[cfg(feature = "extract_jsonpath")] fn extract_json<'a>(expr: &PathCompiled, json_str: &'a str) -> Option> { @@ -63,12 +62,9 @@ impl Utf8Chunked { ndjson::read::infer_iter(values_iter) .map(|d| DataType::from(&d)) - .map_err(|e| PolarsError::ComputeError( - format!("error infering JSON {:?}", e).into(), - )) + .map_err(|e| PolarsError::ComputeError(format!("error infering JSON {:?}", e).into())) } - /// Extracts a JSON value for each row in the Utf8Chunked pub fn json_extract(&self, dtype: Option) -> Result { let dtype = match dtype { @@ -76,14 +72,11 @@ impl Utf8Chunked { None => self.json_infer(None)?, }; - let iter = self - .into_iter() - .map(|x| x.unwrap_or("null")); + let iter = self.into_iter().map(|x| x.unwrap_or("null")); - let array = ndjson::read::deserialize_iter(iter, dtype.to_arrow()) - .map_err(|e| PolarsError::ComputeError( - format!("error deserializing JSON {:?}", e).into(), - ))?; + let array = ndjson::read::deserialize_iter(iter, dtype.to_arrow()).map_err(|e| { + PolarsError::ComputeError(format!("error deserializing JSON {:?}", e).into()) + })?; Series::try_from(("", array)) } diff --git a/py-polars/polars/__init__.py b/py-polars/polars/__init__.py index 6e3feca5f412b..07de1fd44277a 100644 --- a/py-polars/polars/__init__.py +++ b/py-polars/polars/__init__.py @@ -24,6 +24,7 @@ def version() -> str: Date, Datetime, Duration, + Field, Float32, Float64, Int8, @@ -33,7 +34,6 @@ def version() -> str: List, Null, Object, - Field, Struct, Time, UInt8, diff --git a/py-polars/polars/datatypes.py b/py-polars/polars/datatypes.py index a66e4fc176d96..7b86b6d4368d6 100644 --- a/py-polars/polars/datatypes.py +++ b/py-polars/polars/datatypes.py @@ -139,10 +139,7 @@ def __init__(self, name: str, dtype: Type[DataType]): self.dtype = py_type_to_dtype(dtype) def __eq__(self, other: "Field") -> bool: # type: ignore - return ( - (self.name == other.name) & - (self.dtype == other.dtype) - ) + return (self.name == other.name) & (self.dtype == other.dtype) def __repr__(self) -> str: if isinstance(self.dtype, type): diff --git a/py-polars/polars/internals/series.py b/py-polars/polars/internals/series.py index e052320a4ba1c..d494e232dab35 100644 --- a/py-polars/polars/internals/series.py +++ b/py-polars/polars/internals/series.py @@ -4064,7 +4064,9 @@ def json_path_select(self, json_path: str) -> Series: """ return wrap_s(self._s.str_json_path_select(json_path)) - def json_path_extract(self, json_path: str, dtype: Optional[Type[DataType]] = None) -> Series: + def json_path_extract( + self, json_path: str, dtype: Optional[Type[DataType]] = None + ) -> Series: """ Extracts the JSON fields with provided JsonPath expression, returning an appropriately typed Series. The data type can optionally be provided to @@ -4087,7 +4089,7 @@ def json_path_extract(self, json_path: str, dtype: Optional[Type[DataType]] = No -------- >>> s = pl.Series("a", ['{"b": null}', '{"b": [5]}', '{"b": [1, 2]}', None]) - >>> s.str.json_path_extract('$.b[0]') + >>> s.str.json_path_extract("$.b[0]") shape: (4,) Series: '' [i64] [ @@ -4096,7 +4098,7 @@ def json_path_extract(self, json_path: str, dtype: Optional[Type[DataType]] = No 1 null ] - >>> s.str.json_path_extract('$.b[-1]') + >>> s.str.json_path_extract("$.b[-1]") shape: (4,) Series: '' [i64] [ diff --git a/py-polars/src/conversion.rs b/py-polars/src/conversion.rs index 4e2c758786fd3..605b699ba4641 100644 --- a/py-polars/src/conversion.rs +++ b/py-polars/src/conversion.rs @@ -269,13 +269,11 @@ impl ToPyObject for Wrap { DataType::Time => pl.getattr("Time").unwrap().into(), DataType::Struct(fields) => { let field_class = pl.getattr("Field").unwrap(); - let iter = fields - .iter() - .map(|fld| { - let name = fld.name().clone(); - let dtype = Wrap(fld.data_type().clone()).to_object(py); - field_class.call1((name, dtype,)).unwrap() - }); + let iter = fields.iter().map(|fld| { + let name = fld.name().clone(); + let dtype = Wrap(fld.data_type().clone()).to_object(py); + field_class.call1((name, dtype)).unwrap() + }); let fields = PyList::new(py, iter); let struct_class = pl.getattr("Struct").unwrap(); struct_class.call1((fields,)).unwrap().into() @@ -316,8 +314,7 @@ impl FromPyObject<'_> for Wrap { impl FromPyObject<'_> for Wrap { fn extract(ob: &PyAny) -> PyResult { let name = ob.getattr("name")?.str()?.to_str()?; - let dtype = ob.getattr("dtype")? - .extract::>()?; + let dtype = ob.getattr("dtype")?.extract::>()?; Ok(Wrap(Field::new(name, dtype.0))) } } @@ -327,7 +324,8 @@ impl FromPyObject<'_> for Wrap { let type_name = ob.get_type().name()?; let dtype = match type_name { - "type" => { // just the class, not an object + "type" => { + // just the class, not an object let name = ob.getattr("__name__")?.str()?.to_str()?; match name { "UInt8" => DataType::UInt8, @@ -350,14 +348,14 @@ impl FromPyObject<'_> for Wrap { "Object" => DataType::Object("unknown"), "List" => DataType::List(Box::new(DataType::Boolean)), "Null" => DataType::Null, - dt => panic!("{} not expected as Python type for dtype conversion", dt) + dt => panic!("{} not expected as Python type for dtype conversion", dt), } - }, + } "List" => { let inner = ob.getattr("inner")?; let inner = inner.extract::>()?; DataType::List(Box::new(inner.0)) - }, + } "Struct" => { let fields = ob.getattr("fields")?; let fields = fields diff --git a/py-polars/src/series.rs b/py-polars/src/series.rs index 36a126225a7d6..9b3d195160e7f 100644 --- a/py-polars/src/series.rs +++ b/py-polars/src/series.rs @@ -1077,9 +1077,7 @@ impl PySeries { pub fn str_json_infer(&self, py: Python, number_of_rows: Option) -> PyResult { let ca = self.series.utf8().map_err(PyPolarsErr::from)?; - let dtype = ca - .json_infer(number_of_rows) - .map_err(PyPolarsErr::from)?; + let dtype = ca.json_infer(number_of_rows).map_err(PyPolarsErr::from)?; Ok(Wrap(dtype.clone()).to_object(py)) } @@ -1101,7 +1099,11 @@ impl PySeries { Ok(s.into()) } - pub fn str_json_path_extract(&self, path: &str, dtype: Option>) -> PyResult { + pub fn str_json_path_extract( + &self, + path: &str, + dtype: Option>, + ) -> PyResult { let ca = self.series.utf8().map_err(PyPolarsErr::from)?; let s = ca .json_path_extract(path, dtype.map(|x| x.0))