Best Python code snippet using refurb_python
test_series_string.py
Source:test_series_string.py
...36 None,37 np.NaN,38 ]39 )40 def check_func(self, func, almost=False):41 self.check_func_on_series(func, self.pser, almost=almost)42 def check_func_on_series(self, func, pser, almost=False):43 self.assert_eq(func(ps.from_pandas(pser)), func(pser), almost=almost)44 def test_string_add_str_num(self):45 pdf = pd.DataFrame(dict(col1=["a"], col2=[1]))46 psdf = ps.from_pandas(pdf)47 with self.assertRaises(TypeError):48 psdf["col1"] + psdf["col2"]49 def test_string_add_assign(self):50 pdf = pd.DataFrame(dict(col1=["a", "b", "c"], col2=["1", "2", "3"]))51 psdf = ps.from_pandas(pdf)52 psdf["col1"] += psdf["col2"]53 pdf["col1"] += pdf["col2"]54 self.assert_eq(psdf["col1"], pdf["col1"])55 def test_string_add_str_str(self):56 pdf = pd.DataFrame(dict(col1=["a", "b", "c"], col2=["1", "2", "3"]))57 psdf = ps.from_pandas(pdf)58 # TODO: Fix the Series names59 self.assert_eq(psdf["col1"] + psdf["col2"], pdf["col1"] + pdf["col2"])60 self.assert_eq(psdf["col2"] + psdf["col1"], pdf["col2"] + pdf["col1"])61 def test_string_add_str_lit(self):62 pdf = pd.DataFrame(dict(col1=["a", "b", "c"]))63 psdf = ps.from_pandas(pdf)64 self.assert_eq(psdf["col1"] + "_lit", pdf["col1"] + "_lit")65 self.assert_eq("_lit" + psdf["col1"], "_lit" + pdf["col1"])66 def test_string_capitalize(self):67 self.check_func(lambda x: x.str.capitalize())68 def test_string_title(self):69 self.check_func(lambda x: x.str.title())70 def test_string_lower(self):71 self.check_func(lambda x: x.str.lower())72 def test_string_upper(self):73 self.check_func(lambda x: x.str.upper())74 def test_string_swapcase(self):75 self.check_func(lambda x: x.str.swapcase())76 def test_string_startswith(self):77 pattern = "car"78 self.check_func(lambda x: x.str.startswith(pattern))79 self.check_func(lambda x: x.str.startswith(pattern, na=False))80 def test_string_endswith(self):81 pattern = "s"82 self.check_func(lambda x: x.str.endswith(pattern))83 self.check_func(lambda x: x.str.endswith(pattern, na=False))84 def test_string_strip(self):85 self.check_func(lambda x: x.str.strip())86 self.check_func(lambda x: x.str.strip("es\t"))87 self.check_func(lambda x: x.str.strip("1"))88 def test_string_lstrip(self):89 self.check_func(lambda x: x.str.lstrip())90 self.check_func(lambda x: x.str.lstrip("\n1le"))91 self.check_func(lambda x: x.str.lstrip("s"))92 def test_string_rstrip(self):93 self.check_func(lambda x: x.str.rstrip())94 self.check_func(lambda x: x.str.rstrip("\t ec"))95 self.check_func(lambda x: x.str.rstrip("0"))96 def test_string_get(self):97 self.check_func(lambda x: x.str.get(6))98 self.check_func(lambda x: x.str.get(-1))99 def test_string_isalnum(self):100 self.check_func(lambda x: x.str.isalnum())101 def test_string_isalpha(self):102 self.check_func(lambda x: x.str.isalpha())103 def test_string_isdigit(self):104 self.check_func(lambda x: x.str.isdigit())105 def test_string_isspace(self):106 self.check_func(lambda x: x.str.isspace())107 def test_string_islower(self):108 self.check_func(lambda x: x.str.islower())109 def test_string_isupper(self):110 self.check_func(lambda x: x.str.isupper())111 def test_string_istitle(self):112 self.check_func(lambda x: x.str.istitle())113 def test_string_isnumeric(self):114 self.check_func(lambda x: x.str.isnumeric())115 def test_string_isdecimal(self):116 self.check_func(lambda x: x.str.isdecimal())117 def test_string_cat(self):118 psser = ps.from_pandas(self.pser)119 with self.assertRaises(NotImplementedError):120 psser.str.cat()121 def test_string_center(self):122 self.check_func(lambda x: x.str.center(0))123 self.check_func(lambda x: x.str.center(10))124 self.check_func(lambda x: x.str.center(10, "x"))125 def test_string_contains(self):126 self.check_func(lambda x: x.str.contains("le", regex=False))127 self.check_func(lambda x: x.str.contains("White", case=True, regex=False))128 self.check_func(lambda x: x.str.contains("apples|carrots", regex=True))129 self.check_func(lambda x: x.str.contains("BANANAS", flags=re.IGNORECASE, na=False))130 def test_string_count(self):131 self.check_func(lambda x: x.str.count("wh|Wh"))132 self.check_func(lambda x: x.str.count("WH", flags=re.IGNORECASE))133 def test_string_decode(self):134 psser = ps.from_pandas(self.pser)135 with self.assertRaises(NotImplementedError):136 psser.str.decode("utf-8")137 def test_string_encode(self):138 psser = ps.from_pandas(self.pser)139 with self.assertRaises(NotImplementedError):140 psser.str.encode("utf-8")141 def test_string_extract(self):142 psser = ps.from_pandas(self.pser)143 with self.assertRaises(NotImplementedError):144 psser.str.extract("pat")145 def test_string_extractall(self):146 psser = ps.from_pandas(self.pser)147 with self.assertRaises(NotImplementedError):148 psser.str.extractall("pat")149 def test_string_find(self):150 self.check_func(lambda x: x.str.find("a"))151 self.check_func(lambda x: x.str.find("a", start=3))152 self.check_func(lambda x: x.str.find("a", start=0, end=1))153 def test_string_findall(self):154 self.check_func_on_series(lambda x: x.str.findall("es|as").apply(str), self.pser[:-1])155 self.check_func_on_series(156 lambda x: x.str.findall("wh.*", flags=re.IGNORECASE).apply(str), self.pser[:-1]157 )158 def test_string_index(self):159 pser = pd.Series(["tea", "eat"])160 self.check_func_on_series(lambda x: x.str.index("ea"), pser)161 with self.assertRaises(Exception):162 self.check_func_on_series(lambda x: x.str.index("ea", start=0, end=2), pser)163 with self.assertRaises(Exception):164 self.check_func(lambda x: x.str.index("not-found"))165 def test_string_join(self):166 pser = pd.Series([["a", "b", "c"], ["xx", "yy", "zz"]])167 self.check_func_on_series(lambda x: x.str.join("-"), pser)168 self.check_func(lambda x: x.str.join("-"))169 def test_string_len(self):170 self.check_func(lambda x: x.str.len())171 pser = pd.Series([["a", "b", "c"], ["xx"], []])172 self.check_func_on_series(lambda x: x.str.len(), pser)173 def test_string_ljust(self):174 self.check_func(lambda x: x.str.ljust(0))175 self.check_func(lambda x: x.str.ljust(10))176 self.check_func(lambda x: x.str.ljust(30, "x"))177 def test_string_match(self):178 self.check_func(lambda x: x.str.match("in"))179 self.check_func(lambda x: x.str.match("apples|carrots", na=False))180 self.check_func(lambda x: x.str.match("White", case=True))181 self.check_func(lambda x: x.str.match("BANANAS", flags=re.IGNORECASE))182 def test_string_normalize(self):183 self.check_func(lambda x: x.str.normalize("NFC"))184 self.check_func(lambda x: x.str.normalize("NFKD"))185 def test_string_pad(self):186 self.check_func(lambda x: x.str.pad(10))187 self.check_func(lambda x: x.str.pad(10, side="both"))188 self.check_func(lambda x: x.str.pad(10, side="right", fillchar="-"))189 def test_string_partition(self):190 with self.assertRaises(NotImplementedError):191 self.check_func(lambda x: x.str.partition())192 def test_string_repeat(self):193 self.check_func(lambda x: x.str.repeat(repeats=3))194 with self.assertRaises(TypeError):195 self.check_func(lambda x: x.str.repeat(repeats=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))196 def test_string_replace(self):197 self.check_func(lambda x: x.str.replace("a.", "xx", regex=True))198 self.check_func(lambda x: x.str.replace("a.", "xx", regex=False))199 self.check_func(lambda x: x.str.replace("ing", "0", flags=re.IGNORECASE))200 # reverse every lowercase word201 def repl(m):202 return m.group(0)[::-1]203 self.check_func(lambda x: x.str.replace(r"[a-z]+", repl))204 # compiled regex with flags205 regex_pat = re.compile(r"WHITESPACE", flags=re.IGNORECASE)206 self.check_func(lambda x: x.str.replace(regex_pat, "---"))207 def test_string_rfind(self):208 self.check_func(lambda x: x.str.rfind("a"))209 self.check_func(lambda x: x.str.rfind("a", start=3))210 self.check_func(lambda x: x.str.rfind("a", start=0, end=1))211 def test_string_rindex(self):212 pser = pd.Series(["teatea", "eateat"])213 self.check_func_on_series(lambda x: x.str.rindex("ea"), pser)214 with self.assertRaises(Exception):215 self.check_func_on_series(lambda x: x.str.rindex("ea", start=0, end=2), pser)216 with self.assertRaises(Exception):217 self.check_func(lambda x: x.str.rindex("not-found"))218 def test_string_rjust(self):219 self.check_func(lambda x: x.str.rjust(0))220 self.check_func(lambda x: x.str.rjust(10))221 self.check_func(lambda x: x.str.rjust(30, "x"))222 def test_string_rpartition(self):223 with self.assertRaises(NotImplementedError):224 self.check_func(lambda x: x.str.rpartition())225 def test_string_slice(self):226 self.check_func(lambda x: x.str.slice(start=1))227 self.check_func(lambda x: x.str.slice(stop=3))228 self.check_func(lambda x: x.str.slice(step=2))229 self.check_func(lambda x: x.str.slice(start=0, stop=5, step=3))230 def test_string_slice_replace(self):231 self.check_func(lambda x: x.str.slice_replace(1, repl="X"))232 self.check_func(lambda x: x.str.slice_replace(stop=2, repl="X"))233 self.check_func(lambda x: x.str.slice_replace(start=1, stop=3, repl="X"))234 def test_string_split(self):235 self.check_func_on_series(lambda x: repr(x.str.split()), self.pser[:-1])236 self.check_func_on_series(lambda x: repr(x.str.split(r"p*")), self.pser[:-1])237 pser = pd.Series(["This is a sentence.", "This-is-a-long-word."])238 self.check_func_on_series(lambda x: repr(x.str.split(n=2)), pser)239 self.check_func_on_series(lambda x: repr(x.str.split(pat="-", n=2)), pser)240 self.check_func_on_series(lambda x: x.str.split(n=2, expand=True), pser, almost=True)241 with self.assertRaises(NotImplementedError):242 self.check_func(lambda x: x.str.split(expand=True))243 def test_string_rsplit(self):244 self.check_func_on_series(lambda x: repr(x.str.rsplit()), self.pser[:-1])245 self.check_func_on_series(lambda x: repr(x.str.rsplit(r"p*")), self.pser[:-1])246 pser = pd.Series(["This is a sentence.", "This-is-a-long-word."])247 self.check_func_on_series(lambda x: repr(x.str.rsplit(n=2)), pser)248 self.check_func_on_series(lambda x: repr(x.str.rsplit(pat="-", n=2)), pser)249 self.check_func_on_series(lambda x: x.str.rsplit(n=2, expand=True), pser, almost=True)250 with self.assertRaises(NotImplementedError):251 self.check_func(lambda x: x.str.rsplit(expand=True))252 def test_string_translate(self):253 m = str.maketrans({"a": "X", "e": "Y", "i": None})254 self.check_func(lambda x: x.str.translate(m))255 def test_string_wrap(self):256 self.check_func(lambda x: x.str.wrap(5))257 self.check_func(lambda x: x.str.wrap(5, expand_tabs=False))258 self.check_func(lambda x: x.str.wrap(5, replace_whitespace=False))259 self.check_func(lambda x: x.str.wrap(5, drop_whitespace=False))260 self.check_func(lambda x: x.str.wrap(5, break_long_words=False))261 self.check_func(lambda x: x.str.wrap(5, break_on_hyphens=False))262 def test_string_zfill(self):263 self.check_func(lambda x: x.str.zfill(10))264 def test_string_get_dummies(self):265 with self.assertRaises(NotImplementedError):266 self.check_func(lambda x: x.str.get_dummies())267if __name__ == "__main__":268 import unittest269 from pyspark.pandas.tests.test_series_string import * # noqa: F401270 try:271 import xmlrunner # type: ignore[import]272 testRunner = xmlrunner.XMLTestRunner(output="target/test-reports", verbosity=2)273 except ImportError:274 testRunner = None...
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!!