Best Python code snippet using pandera_python
date_utils.py
Source:date_utils.py
1# -*- coding: utf-8 -*-2import math3import calendar4from datetime import date, datetime, time5import pytz6from dateutil.relativedelta import relativedelta7from . import ustr8def get_month(date):9 ''' Compute the month dates range on which the 'date' parameter belongs to.10 :param date: A datetime.datetime or datetime.date object.11 :return: A tuple (date_from, date_to) having the same object type as the 'date' parameter.12 '''13 date_from = type(date)(date.year, date.month, 1)14 date_to = type(date)(date.year, date.month, calendar.monthrange(date.year, date.month)[1])15 return date_from, date_to16def get_quarter_number(date):17 ''' Get the number of the quarter on which the 'date' parameter belongs to.18 :param date: A datetime.datetime or datetime.date object.19 :return: A [1-4] integer.20 '''21 return math.ceil(date.month / 3)22def get_quarter(date):23 ''' Compute the quarter dates range on which the 'date' parameter belongs to.24 :param date: A datetime.datetime or datetime.date object.25 :return: A tuple (date_from, date_to) having the same object type as the 'date' parameter.26 '''27 quarter_number = get_quarter_number(date)28 month_from = ((quarter_number - 1) * 3) + 129 date_from = type(date)(date.year, month_from, 1)30 date_to = (date_from + relativedelta(months=2))31 date_to = date_to.replace(day=calendar.monthrange(date_to.year, date_to.month)[1])32 return date_from, date_to33def get_fiscal_year(date, day=31, month=12):34 ''' Compute the fiscal year dates range on which the 'date' parameter belongs to.35 A fiscal year is the period used by governments for accounting purposes and vary between countries.36 By default, calling this method with only one parameter gives the calendar year because the ending date of the37 fiscal year is set to the YYYY-12-31.38 :param date: A datetime.datetime or datetime.date object.39 :param day: The day of month the fiscal year ends.40 :param month: The month of year the fiscal year ends.41 :return: A tuple (date_from, date_to) having the same object type as the 'date' parameter.42 '''43 max_day = calendar.monthrange(date.year, month)[1]44 date_to = type(date)(date.year, month, min(day, max_day))45 if date <= date_to:46 date_from = date_to - relativedelta(years=1)47 date_from += relativedelta(days=1)48 else:49 date_from = date_to + relativedelta(days=1)50 max_day = calendar.monthrange(date_to.year + 1, date_to.month)[1]51 date_to = type(date)(date.year + 1, month, min(day, max_day))52 return date_from, date_to53def start_of(value, granularity):54 """55 Get start of a time period from a date or a datetime.56 :param value: initial date or datetime.57 :param granularity: type of period in string, can be year, quarter, month, week, day or hour.58 :return: a date/datetime object corresponding to the start of the specified period.59 """60 is_datetime = isinstance(value, datetime)61 if granularity == "year":62 result = value.replace(month=1, day=1)63 elif granularity == "quarter":64 # Q1 = Jan 1st65 # Q2 = Apr 1st66 # Q3 = Jul 1st67 # Q4 = Oct 1st68 result = get_quarter(value)[0]69 elif granularity == "month":70 result = value.replace(day=1)71 elif granularity == 'week':72 # `calendar.weekday` uses ISO8601 for start of week reference, this means that73 # by default MONDAY is the first day of the week and SUNDAY is the last.74 result = value - relativedelta(days=calendar.weekday(value.year, value.month, value.day))75 elif granularity == "day":76 result = value77 elif granularity == "hour" and is_datetime:78 return datetime.combine(value, time.min).replace(hour=value.hour)79 elif is_datetime:80 raise ValueError(81 "Granularity must be year, quarter, month, week, day or hour for value %s" % value82 )83 else:84 raise ValueError(85 "Granularity must be year, quarter, month, week or day for value %s" % value86 )87 return datetime.combine(result, time.min) if is_datetime else result88def end_of(value, granularity):89 """90 Get end of a time period from a date or a datetime.91 :param value: initial date or datetime.92 :param granularity: Type of period in string, can be year, quarter, month, week, day or hour.93 :return: A date/datetime object corresponding to the start of the specified period.94 """95 is_datetime = isinstance(value, datetime)96 if granularity == "year":97 result = value.replace(month=12, day=31)98 elif granularity == "quarter":99 # Q1 = Mar 31st100 # Q2 = Jun 30th101 # Q3 = Sep 30th102 # Q4 = Dec 31st103 result = get_quarter(value)[1]104 elif granularity == "month":105 result = value + relativedelta(day=1, months=1, days=-1)106 elif granularity == 'week':107 # `calendar.weekday` uses ISO8601 for start of week reference, this means that108 # by default MONDAY is the first day of the week and SUNDAY is the last.109 result = value + relativedelta(days=6-calendar.weekday(value.year, value.month, value.day))110 elif granularity == "day":111 result = value112 elif granularity == "hour" and is_datetime:113 return datetime.combine(value, time.max).replace(hour=value.hour)114 elif is_datetime:115 raise ValueError(116 "Granularity must be year, quarter, month, week, day or hour for value %s" % value117 )118 else:119 raise ValueError(120 "Granularity must be year, quarter, month, week or day for value %s" % value121 )122 return datetime.combine(result, time.max) if is_datetime else result123def add(value, *args, **kwargs):124 """125 Return the sum of ``value`` and a :class:`relativedelta`.126 :param value: initial date or datetime.127 :param args: positional args to pass directly to :class:`relativedelta`.128 :param kwargs: keyword args to pass directly to :class:`relativedelta`.129 :return: the resulting date/datetime.130 """131 return value + relativedelta(*args, **kwargs)132def subtract(value, *args, **kwargs):133 """134 Return the difference between ``value`` and a :class:`relativedelta`.135 :param value: initial date or datetime.136 :param args: positional args to pass directly to :class:`relativedelta`.137 :param kwargs: keyword args to pass directly to :class:`relativedelta`.138 :return: the resulting date/datetime.139 """140 return value - relativedelta(*args, **kwargs)141def json_default(obj):142 """143 Properly serializes date and datetime objects.144 """145 from odoo import fields146 if isinstance(obj, date):147 if isinstance(obj, datetime):148 return fields.Datetime.to_string(obj)149 return fields.Date.to_string(obj)150 return ustr(obj)151def date_range(start, end, step=relativedelta(months=1)):152 """Date range generator with a step interval.153 :param start datetime: begining date of the range.154 :param end datetime: ending date of the range.155 :param step relativedelta: interval of the range.156 :return: a range of datetime from start to end.157 :rtype: Iterator[datetime]158 """159 are_naive = start.tzinfo is None and end.tzinfo is None160 are_utc = start.tzinfo == pytz.utc and end.tzinfo == pytz.utc161 # Cases with miscellenous timezone are more complexe because of DST.162 are_others = start.tzinfo and end.tzinfo and not are_utc163 if are_others:164 if start.tzinfo.zone != end.tzinfo.zone:165 raise ValueError("Timezones of start argument and end argument seem inconsistent")166 if not are_naive and not are_utc and not are_others:167 raise ValueError("Timezones of start argument and end argument mismatch")168 if start > end:169 raise ValueError("start > end, start date must be before end")170 if start == start + step:171 raise ValueError("Looks like step is null")172 if start.tzinfo:173 localize = start.tzinfo.localize174 else:175 localize = lambda dt: dt176 dt = start.replace(tzinfo=None)177 end = end.replace(tzinfo=None)178 while dt <= end:179 yield localize(dt)...
column_guesser.py
Source:column_guesser.py
1#-------------------------------------------------------------------------2# Copyright (c) Microsoft Corporation. All rights reserved.3# Licensed under the MIT License. See License.txt in the project root for4# license information.5#--------------------------------------------------------------------------6from datetime import timedelta, datetime7"""8Splits tabular data in the form of a list of rows into columns;9makes guesses about the role of each column for plotting purposes10(X values, Y values, and text labels).11"""12class Column(list):13 "Store a column of tabular data; record its name and whether it is numeric"14 # Object constructor15 def __init__(self, idx=None,name='', col=None, is_quantity=True, is_datetime=True, **kwargs):16 if col is not None:17 self.is_quantity = col.is_quantity18 self.is_datetime = col.is_datetime19 self.idx = col.idx20 self.name = col.name21 else:22 self.is_quantity = is_quantity23 self.is_datetime = is_datetime24 self.idx = idx25 self.name = name26 super(Column, self).__init__(**kwargs)27class ChartSubTable(dict):28 def __init__(self, col_x=None, col_y=None, name=None, mapping=None, **kwargs):29 self.col_x = col_x30 self.col_y = col_y31 self.name = name32 super(ChartSubTable, self).__init__(**kwargs)33 if mapping:34 self.update(mapping)35def is_quantity(val):36 """Is ``val`` a quantity (int, float, datetime, etc) (not str, bool)?37 38 Relies on presence of __sub__.39 """40 return hasattr(val, "__sub__")41class ColumnGuesserMixin(object):42 """43 plot: [x, y, y...], y44 pie: ... y45 """46 DATAFRAME_QUNATITY_TYPES = ["int64", "float64", "datetime64[ns]", "timedelta64[ns]", "int32",]47 DATAFRAME_TIME_TYPES = ["datetime64[ns]", "timedelta64[ns]",]48 def _build_chart_sub_tables(self, name=None, x_type='first'):49 self.chart_sub_tables = []50 self._build_columns(name, without_data=True)51 x_col_idx = None52 if x_type =='first':53 x_col_idx = 054 elif x_type == 'quantity':55 for idx,c in enumerate(self.columns):56 if c.is_quantity:57 x_col_idx = idx58 break59 elif x_type == 'datetime':60 for idx,c in enumerate(self.columns):61 if c.is_datetime:62 x_col_idx = idx63 break64 if x_col_idx is None:65 return66 quantity_columns = [c for idx,c in enumerate(self.columns) if idx != x_col_idx and c.is_quantity]67 non_quantity_columns = [c for idx,c in enumerate(self.columns) if idx != x_col_idx and not c.is_quantity]68 rows = self69 if self.columns[x_col_idx].is_quantity:70 rows = sorted(self, key=lambda row: row[x_col_idx])71 col_x = Column(col=self.columns[x_col_idx])72 for row in rows:73 if row[x_col_idx] not in col_x:74 col_x.append(row[x_col_idx])75 chart_sub_tables_dict = {}76 for row in rows:77 for qcol in quantity_columns:78 if len(non_quantity_columns) > 0:79 sub_table_name = ':'.join([row[col.idx] for col in non_quantity_columns]) + ':' + qcol.name80 else:81 sub_table_name = qcol.name82 chart_sub_table = chart_sub_tables_dict.get(sub_table_name)83 if chart_sub_table is None:84 chart_sub_table = chart_sub_tables_dict[sub_table_name] = ChartSubTable(85 name=sub_table_name, 86 col_x=Column(col=self.columns[x_col_idx]), 87 col_y=Column(col=qcol),88 mapping=dict(zip(col_x, [None for i in range(len(col_x))])))89 chart_sub_table[row[x_col_idx]] = row[qcol.idx]90 self.chart_sub_tables = list(chart_sub_tables_dict.values())91 def _build_columns(self, name=None, without_data=False):92 self.x = Column()93 self.ys = []94 rows = self95 if name:96 idx = self.columns_name.index(name)97 rows = sorted(self, key=lambda row: row[idx]) # sort by index98 self.columns = [Column(idx, name) for (idx, name) in enumerate(self.columns_name)]99 if len(self.columns_datafarme_type) > 0:100 for col in self.columns:101 datafarme_type = self.columns_datafarme_type[col.idx]102 col.is_quantity = datafarme_type in self.DATAFRAME_QUNATITY_TYPES103 col.is_datetime = datafarme_type in self.DATAFRAME_TIME_TYPES104 if without_data:105 return106 for row in rows:107 for (col_idx, col_val) in enumerate(row):108 col = self.columns[col_idx]109 if not without_data:110 col.append(col_val)111 if len(self.columns_datafarme_type) == 0:112 if (col_val is not None) and (not is_quantity(col_val)):113 col.is_quantity = False114 col.is_datetime = False115 elif not isinstance(col_val, datetime):116 col.is_datetime = False117 def _get_y(self):118 for idx in range(len(self.columns) - 1, -1, -1):119 if self.columns[idx].is_quantity:120 self.ys.insert(0, self.columns.pop(idx))121 return True122 def _get_x(self):123 for idx in range(len(self.columns)):124 if self.columns[idx].is_quantity:125 self.x = self.columns.pop(idx)126 return True127 def _get_xlabel(self, xlabel_sep=" "):128 self.xlabels = []129 if self.columns:130 for row_idx in range(len(self.columns[0])):131 self.xlabels.append(xlabel_sep.join(str(c[row_idx]) for c in self.columns))132 self.xlabel = ", ".join(c.name for c in self.columns)133 def _get_xlabel(self, xlabel_sep=" ", index=None):134 self.xlabels = []135 if self.columns:136 for row_idx in range(len(self.columns[0])):137 self.xlabels.append(xlabel_sep.join(str(c[row_idx]) for c in self.columns))138 self.xlabel = ", ".join(c.name for c in self.columns)139 def _guess_columns(self):140 self._build_columns()141 self._get_y()142 if not self.ys:143 raise AttributeError("No quantitative columns found for chart")144 def build_columns(self, name=None):145 """146 just build the columns.147 if name specified, first sort row by name148 """149 self._build_columns(name)150 def guess_pie_columns(self, xlabel_sep=" "):151 """152 Assigns x, y, and x labels from the data set for a pie chart.153 154 Pie charts simply use the last quantity column as 155 the pie slice size, and everything else as the156 pie slice labels.157 """158 self._guess_columns()159 self._get_xlabel(xlabel_sep)160 def guess_plot_columns(self):161 """162 Assigns ``x`` and ``y`` series from the data set for a plot.163 164 Plots use:165 the rightmost quantity column as a Y series166 optionally, the leftmost quantity column as the X series167 any other quantity columns as additional Y series168 """169 self._guess_columns()170 self._get_x()171 while self._get_y():...
test_utils.py
Source:test_utils.py
1"""Tests for the sdv.constraints.utils module."""2from datetime import datetime3import pandas as pd4from sdv.constraints.utils import is_datetime_type5def test_is_datetime_type_with_datetime_series():6 """Test the ``is_datetime_type`` function when a datetime series is passed.7 Expect to return True when a datetime series is passed.8 Input:9 - A pandas.Series of type `datetime64[ns]`10 Output:11 - True12 """13 # Setup14 data = pd.Series([15 pd.to_datetime('2020-01-01'),16 pd.to_datetime('2020-01-02'),17 pd.to_datetime('2020-01-03')],18 )19 # Run20 is_datetime = is_datetime_type(data)21 # Assert22 assert is_datetime23def test_is_datetime_type_with_datetime():24 """Test the ``is_datetime_type`` function when a datetime is passed.25 Expect to return True when a datetime variable is passed.26 Input:27 - datetime.Datetime28 Output:29 - True30 """31 # Setup32 data = datetime(2020, 1, 1)33 # Run34 is_datetime = is_datetime_type(data)35 # Assert36 assert is_datetime37def test_is_datetime_type_with_pandas_datetime():38 """Test the ``is_datetime_type`` function when a pandas.datetime is passed.39 Expect to return True when a datetime variable is passed.40 Input:41 - pandas.Datetime42 Output:43 - True44 """45 # Setup46 data = pd.to_datetime('2020-01-01')47 # Run48 is_datetime = is_datetime_type(data)49 # Assert50 assert is_datetime51def test_is_datetime_type_with_int():52 """Test the ``is_datetime_type`` function when an int is passed.53 Expect to return False when an int variable is passed.54 Input:55 - int56 Output:57 - False58 """59 # Setup60 data = 261 # Run62 is_datetime = is_datetime_type(data)63 # Assert64 assert is_datetime is False65def test_is_datetime_type_with_string():66 """Test the ``is_datetime_type`` function when a string is passed.67 Expect to return False when a string variable is passed.68 Input:69 - string70 Output:71 - False72 """73 # Setup74 data = 'test'75 # Run76 is_datetime = is_datetime_type(data)77 # Assert78 assert is_datetime is False79def test_is_datetime_type_with_int_series():80 """Test the ``is_datetime_type`` function when an int series is passed.81 Expect to return False when an int series variable is passed.82 Input:83 - pd.Series of type int84 Output:85 - False86 """87 # Setup88 data = pd.Series([1, 2, 3, 4])89 # Run90 is_datetime = is_datetime_type(data)91 # Assert...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!