Skip to content

Commit

Permalink
Fixing formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
cjermain committed May 22, 2022
1 parent e682abc commit e1876e9
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 39 deletions.
2 changes: 1 addition & 1 deletion examples/json_path/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;

Expand Down
19 changes: 6 additions & 13 deletions polars/polars-core/src/chunked_array/strings/json_path.rs
Original file line number Diff line number Diff line change
@@ -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<Cow<'a, str>> {
Expand Down Expand Up @@ -63,27 +62,21 @@ 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<DataType>) -> Result<Series> {
let dtype = match dtype {
Some(dt) => dt,
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))
}
Expand Down
2 changes: 1 addition & 1 deletion py-polars/polars/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def version() -> str:
Date,
Datetime,
Duration,
Field,
Float32,
Float64,
Int8,
Expand All @@ -33,7 +34,6 @@ def version() -> str:
List,
Null,
Object,
Field,
Struct,
Time,
UInt8,
Expand Down
5 changes: 1 addition & 4 deletions py-polars/polars/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 5 additions & 3 deletions py-polars/polars/internals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
[
Expand All @@ -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]
[
Expand Down
24 changes: 11 additions & 13 deletions py-polars/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,11 @@ impl ToPyObject for Wrap<DataType> {
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()
Expand Down Expand Up @@ -316,8 +314,7 @@ impl FromPyObject<'_> for Wrap<QuantileInterpolOptions> {
impl FromPyObject<'_> for Wrap<Field> {
fn extract(ob: &PyAny) -> PyResult<Self> {
let name = ob.getattr("name")?.str()?.to_str()?;
let dtype = ob.getattr("dtype")?
.extract::<Wrap<DataType>>()?;
let dtype = ob.getattr("dtype")?.extract::<Wrap<DataType>>()?;
Ok(Wrap(Field::new(name, dtype.0)))
}
}
Expand All @@ -327,7 +324,8 @@ impl FromPyObject<'_> for Wrap<DataType> {
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,
Expand All @@ -350,14 +348,14 @@ impl FromPyObject<'_> for Wrap<DataType> {
"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::<Wrap<DataType>>()?;
DataType::List(Box::new(inner.0))
},
}
"Struct" => {
let fields = ob.getattr("fields")?;
let fields = fields
Expand Down
10 changes: 6 additions & 4 deletions py-polars/src/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1077,9 +1077,7 @@ impl PySeries {

pub fn str_json_infer(&self, py: Python, number_of_rows: Option<usize>) -> PyResult<PyObject> {
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))
}

Expand All @@ -1101,7 +1099,11 @@ impl PySeries {
Ok(s.into())
}

pub fn str_json_path_extract(&self, path: &str, dtype: Option<Wrap<DataType>>) -> PyResult<Self> {
pub fn str_json_path_extract(
&self,
path: &str,
dtype: Option<Wrap<DataType>>,
) -> PyResult<Self> {
let ca = self.series.utf8().map_err(PyPolarsErr::from)?;
let s = ca
.json_path_extract(path, dtype.map(|x| x.0))
Expand Down

0 comments on commit e1876e9

Please sign in to comment.