How to use _get_to_datetime_fn method in pandera

Best Python code snippet using pandera_python

pandas_engine.py

Source:pandas_engine.py Github

copy

Full Screen

...560 to_datetime_kwargs: Dict[str, Any] = dataclasses.field(561 default_factory=dict, compare=False, repr=False562 )563 @staticmethod564 def _get_to_datetime_fn(obj: Any) -> Callable:565 # NOTE: this is a hack to support pyspark.pandas. This needs to be566 # thoroughly tested, right now pyspark.pandas returns NA when a567 # dtype value can't be coerced into the target dtype.568 to_datetime_fn = pd.to_datetime569 if type(obj).__module__.startswith(570 "pyspark.pandas"571 ): # pragma: no cover572 # pylint: disable=import-outside-toplevel573 import pyspark.pandas as ps574 to_datetime_fn = ps.to_datetime575 if type(obj).__module__.startswith("modin.pandas"):576 # pylint: disable=import-outside-toplevel577 import modin.pandas as mpd578 to_datetime_fn = mpd.to_datetime579 return to_datetime_fn580@Engine.register_dtype(581 equivalents=[582 "time",583 "datetime",584 "datetime64",585 datetime.datetime,586 np.datetime64,587 dtypes.Timestamp,588 dtypes.Timestamp(),589 pd.Timestamp,590 ]591)592@immutable(init=True)593class DateTime(_BaseDateTime, dtypes.Timestamp):594 """Semantic representation of a potentially timezone-aware datetime.595 Uses ``np.dtype("datetime64[ns]")`` for non-timezone aware datetimes and596 :class:`pandas.DatetimeTZDtype` for timezone-aware datetimes.597 """598 type: Optional[_PandasDatetime] = dataclasses.field(599 default=None, init=False600 )601 unit: str = "ns"602 """The precision of the datetime data. Currently limited to "ns"."""603 tz: Optional[datetime.tzinfo] = None604 """The timezone."""605 to_datetime_kwargs: Dict[str, Any] = dataclasses.field(606 default_factory=dict, compare=False, repr=False607 )608 "Any additional kwargs passed to :func:`pandas.to_datetime` for coercion."609 tz_localize_kwargs: Dict[str, Any] = dataclasses.field(610 default_factory=dict, compare=False, repr=False611 )612 "Keyword arguments passed to :func:`pandas.Series.dt.tz_localize` for coercion."613 _default_tz_localize_kwargs = {614 "ambiguous": "infer",615 }616 def __post_init__(self):617 if self.tz is None:618 type_ = np.dtype("datetime64[ns]")619 else:620 type_ = pd.DatetimeTZDtype(self.unit, self.tz)621 # DatetimeTZDtype converted tz to tzinfo for us622 object.__setattr__(self, "tz", type_.tz)623 object.__setattr__(self, "type", type_)624 def _coerce(625 self, data_container: PandasObject, pandas_dtype: Any626 ) -> PandasObject:627 to_datetime_fn = self._get_to_datetime_fn(data_container)628 _tz_localize_kwargs = {629 **self._default_tz_localize_kwargs,630 **self.tz_localize_kwargs,631 }632 def _to_datetime(col: PandasObject) -> PandasObject:633 col = to_datetime_fn(col, **self.to_datetime_kwargs)634 if (635 hasattr(pandas_dtype, "tz")636 and pandas_dtype.tz is not None637 and col.dt.tz is None638 ):639 # localize datetime column so that it's timezone-aware640 col = col.dt.tz_localize(641 pandas_dtype.tz,642 **_tz_localize_kwargs,643 )644 return col.astype(pandas_dtype)645 if isinstance(data_container, pd.DataFrame):646 # pd.to_datetime transforms a df input into a series.647 # We actually want to coerce every columns.648 return data_container.transform(_to_datetime)649 return _to_datetime(data_container)650 @classmethod651 def from_parametrized_dtype(cls, pd_dtype: pd.DatetimeTZDtype):652 """Convert a :class:`pandas.DatetimeTZDtype` to653 a Pandera :class:`pandera.engines.pandas_engine.DateTime`."""654 return cls(unit=pd_dtype.unit, tz=pd_dtype.tz) # type: ignore655 def coerce(self, data_container: PandasObject) -> PandasObject:656 return self._coerce(data_container, pandas_dtype=self.type)657 def coerce_value(self, value: Any) -> Any:658 """Coerce an value to specified datatime type."""659 return self._get_to_datetime_fn(value)(660 value, **self.to_datetime_kwargs661 )662 def __str__(self) -> str:663 if self.type == np.dtype("datetime64[ns]"):664 return "datetime64[ns]"665 return str(self.type)666@Engine.register_dtype(667 equivalents=[668 "date",669 datetime.date,670 dtypes.Date,671 dtypes.Date(),672 ]673)674@immutable(init=True)675class Date(_BaseDateTime, dtypes.Date):676 """Semantic representation of a date data type."""677 type = np.dtype("object")678 to_datetime_kwargs: Dict[str, Any] = dataclasses.field(679 default_factory=dict, compare=False, repr=False680 )681 "Any additional kwargs passed to :func:`pandas.to_datetime` for coercion."682 # define __init__ to please mypy683 def __init__( # pylint:disable=super-init-not-called684 self,685 to_datetime_kwargs: Optional[Dict[str, Any]] = None,686 ) -> None:687 object.__setattr__(688 self, "to_datetime_kwargs", to_datetime_kwargs or {}689 )690 def _coerce(691 self, data_container: PandasObject, pandas_dtype: Any692 ) -> PandasObject:693 to_datetime_fn = self._get_to_datetime_fn(data_container)694 def _to_datetime(col: PandasObject) -> PandasObject:695 col = to_datetime_fn(col, **self.to_datetime_kwargs)696 return col.astype(pandas_dtype).dt.date697 if isinstance(data_container, pd.DataFrame):698 # pd.to_datetime transforms a df input into a series.699 # We actually want to coerce every columns.700 return data_container.transform(_to_datetime)701 return _to_datetime(data_container)702 def coerce(self, data_container: PandasObject) -> PandasObject:703 return self._coerce(data_container, pandas_dtype="datetime64[ns]")704 def coerce_value(self, value: Any) -> Any:705 coerced = self._get_to_datetime_fn(value)(706 value, **self.to_datetime_kwargs707 )708 return coerced.date() if coerced is not None else pd.NaT709 def check( # type: ignore710 self,711 pandera_dtype: DataType,712 data_container: Optional[pd.Series] = None,713 ) -> Union[bool, Iterable[bool]]:714 if not DataType.check(self, pandera_dtype, data_container):715 if data_container is None:716 return False717 else:718 return np.full_like(data_container, False)719 if data_container is None:...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pandera automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful