Source code for ads.type_discovery.datetime_detector
#!/usr/bin/env python# -*- coding: utf-8; -*-# Copyright (c) 2020, 2022 Oracle and/or its affiliates.# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/from__future__importprint_function,absolute_import,divisionimportpandasaspdfromads.common.decorator.runtime_dependencyimport(runtime_dependency,OptionalDependency,)fromads.type_discoveryimportloggerfromads.type_discovery.abstract_detectorimportAbstractTypeDiscoveryDetectorfromads.type_discovery.typed_featureimportDateTimeTypedFeature
[docs]classDateTimeDetector(AbstractTypeDiscoveryDetector):@runtime_dependency(module="datefinder",install_from=OptionalDependency.DATA)def_is_date_time(self,name,values,low_level_type_name):iflow_level_type_name.startswith("datetime64"):returnlambdax:xelse:## if the column/feature contains the word "timestamp" then#iflow_level_type_name.startswith("int")and"timestamp"inname.lower():# either s (max len 10) on ns (max len) 19unit="s"ifvalues.astype("str").str.len().max()<=10else"ns"try:pd.to_datetime(values,unit=unit)returnlambdax:pd.to_datetime(x,unit=unit)except:passifvalues.dtype=="object":try:pd.to_datetime(values,infer_datetime_format=True)datefinder_result=all([bool(list(datefinder.find_dates(str(x))))forxinvalues])ifdatefinder_result:returnlambdax:pd.to_datetime(x,infer_datetime_format=True)except:passreturnNone