How to use register_dataframe_accessor method in pandera

Best Python code snippet using pandera_python

ext.py

Source:ext.py Github

copy

Full Screen

1from typing import List, Tuple2import pandas as pd3@pd.api.extensions.register_dataframe_accessor("tag")4class CaTaggingAccessor:5 def __init__(self, df: pd.DataFrame):6 self._df = df7 def group_by_sentences(self):8 yield from (x[1] for x in self._df.groupby("sentence_id"))9 def group_by_documents(self):10 yield from (x[1] for x in self._df.groupby("document_id"))11 def number_of_sentences(self):12 return len(self._df.groupby("sentence_id"))13 def number_of_documents(self):14 return len(self._df.groupby("document_id"))15 def split_x_y_sentencewise(self) -> Tuple[List[List[str]], List[List[str]]]:16 X = []17 y = []18 for sent in self._df.tag.group_by_sentences():19 words = list(sent["word"])20 labels = list(sent["label"])21 X.append(words)22 y.append(labels)23 return X, y24 def get_times_per_document(self) -> List[int]:25 t = []26 # Right now, we assume that the time per token is the same27 # for a sentence. This might be an invalid assumption28 for df in self._df.tag.group_by_sentences():29 t.append(df["t"].values[0])30 return t31 def group_by_documents_x_y(self) -> Tuple[List[List[List[str]]], List[List[List[str]]]]:32 """Returns a list of documents that each contain a list of sentences and33 their respective labels grouped the same way.34 """35 X = []36 y = []37 for doc in self._df.tag.group_by_documents():38 X_doc = []39 y_doc = []40 for sent in doc.tag.group_by_sentences():41 words = list(sent["word"])42 labels = list(sent["label"])43 X_doc.append(words)44 y_doc.append(labels)45 X.append(X_doc)46 y.append(y_doc)47 return X, y48 def group_by_sentences_x_y(self) -> Tuple[List[List[str]], List[List[str]]]:49 """Returns a list of sentences and their respective labels grouped the same way."""50 X = []51 y = []52 for sent in self._df.tag.group_by_sentences():53 words = list(sent["word"])54 labels = list(sent["label"])55 assert len(words) == len(labels)56 X.append(words)57 y.append(labels)58 return X, y59@pd.api.extensions.register_dataframe_accessor("dclass")60class CaDocumentClassificationAccessor:61 def __init__(self, df: pd.DataFrame):62 self._df = df63 def split_x_y(self) -> Tuple[List[str], List[str]]:64 X = self._df["sentence"]65 y = self._df["label"]66 return X.values.tolist(), y.values.tolist()67 def get_time_per_sentence(self) -> List[int]:68 return self._df["t"].values.tolist()69@pd.api.extensions.register_dataframe_accessor("pair")70class CaPairAccessor:71 def __init__(self, df: pd.DataFrame):72 self._df = df73 def split_args_y(self) -> Tuple[List[str], List[str], List[str]]:74 args1 = self._df["arg1"].values.tolist()75 args2 = self._df["arg2"].values.tolist()76 label = self._df["label"].values.tolist()77 return args1, args2, label78 def get_time_per_sentence(self) -> List[int]:...

Full Screen

Full Screen

__init__.pyi

Source:__init__.pyi Github

copy

Full Screen

1from pandas._libs.lib import no_default as no_default2from pandas.core.accessor import register_dataframe_accessor as register_dataframe_accessor, register_index_accessor as register_index_accessor, register_series_accessor as register_series_accessor3from pandas.core.algorithms import take as take4from pandas.core.arrays import ExtensionArray as ExtensionArray, ExtensionScalarOpsMixin as ExtensionScalarOpsMixin...

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