How to use _mask method in pandera

Best Python code snippet using pandera_python

test_mrecords.py

Source:test_mrecords.py Github

copy

Full Screen

...129 val = ma.array([10, 20, 30], mask=[1, 0, 0])130 rdata['num'] = val131 assert_equal(rdata.num, val)132 assert_equal(rdata.num.mask, [1, 0, 0])133 def test_set_fields_mask(self):134 # Tests setting the mask of a field.135 base = self.base.copy()136 # This one has already a mask....137 mbase = base.view(mrecarray)138 mbase['a'][-2] = masked139 assert_equal(mbase.a, [1, 2, 3, 4, 5])140 assert_equal(mbase.a._mask, [0, 1, 0, 1, 1])141 # This one has not yet142 mbase = fromarrays([np.arange(5), np.random.rand(5)],143 dtype=[('a', int), ('b', float)])144 mbase['a'][-2] = masked145 assert_equal(mbase.a, [0, 1, 2, 3, 4])146 assert_equal(mbase.a._mask, [0, 0, 0, 1, 0])147 def test_set_mask(self):148 base = self.base.copy()149 mbase = base.view(mrecarray)150 # Set the mask to True .......................151 mbase.mask = masked152 assert_equal(ma.getmaskarray(mbase['b']), [1]*5)153 assert_equal(mbase['a']._mask, mbase['b']._mask)154 assert_equal(mbase['a']._mask, mbase['c']._mask)155 assert_equal(mbase._mask.tolist(),156 np.array([(1, 1, 1)]*5, dtype=bool))157 # Delete the mask ............................158 mbase.mask = nomask159 assert_equal(ma.getmaskarray(mbase['c']), [0]*5)160 assert_equal(mbase._mask.tolist(),161 np.array([(0, 0, 0)]*5, dtype=bool))162 def test_set_mask_fromarray(self):163 base = self.base.copy()164 mbase = base.view(mrecarray)165 # Sets the mask w/ an array166 mbase.mask = [1, 0, 0, 0, 1]167 assert_equal(mbase.a.mask, [1, 0, 0, 0, 1])168 assert_equal(mbase.b.mask, [1, 0, 0, 0, 1])169 assert_equal(mbase.c.mask, [1, 0, 0, 0, 1])170 # Yay, once more !171 mbase.mask = [0, 0, 0, 0, 1]172 assert_equal(mbase.a.mask, [0, 0, 0, 0, 1])173 assert_equal(mbase.b.mask, [0, 0, 0, 0, 1])174 assert_equal(mbase.c.mask, [0, 0, 0, 0, 1])175 def test_set_mask_fromfields(self):176 mbase = self.base.copy().view(mrecarray)177 nmask = np.array(178 [(0, 1, 0), (0, 1, 0), (1, 0, 1), (1, 0, 1), (0, 0, 0)],179 dtype=[('a', bool), ('b', bool), ('c', bool)])180 mbase.mask = nmask181 assert_equal(mbase.a.mask, [0, 0, 1, 1, 0])182 assert_equal(mbase.b.mask, [1, 1, 0, 0, 0])183 assert_equal(mbase.c.mask, [0, 0, 1, 1, 0])184 # Reinitialize and redo185 mbase.mask = False186 mbase.fieldmask = nmask187 assert_equal(mbase.a.mask, [0, 0, 1, 1, 0])188 assert_equal(mbase.b.mask, [1, 1, 0, 0, 0])189 assert_equal(mbase.c.mask, [0, 0, 1, 1, 0])190 def test_set_elements(self):191 base = self.base.copy()192 # Set an element to mask .....................193 mbase = base.view(mrecarray).copy()194 mbase[-2] = masked195 assert_equal(196 mbase._mask.tolist(),197 np.array([(0, 0, 0), (1, 1, 1), (0, 0, 0), (1, 1, 1), (1, 1, 1)],198 dtype=bool))199 # Used to be mask, now it's recordmask!200 assert_equal(mbase.recordmask, [0, 1, 0, 1, 1])201 # Set slices .................................202 mbase = base.view(mrecarray).copy()203 mbase[:2] = (5, 5, 5)204 assert_equal(mbase.a._data, [5, 5, 3, 4, 5])205 assert_equal(mbase.a._mask, [0, 0, 0, 0, 1])206 assert_equal(mbase.b._data, [5., 5., 3.3, 4.4, 5.5])207 assert_equal(mbase.b._mask, [0, 0, 0, 0, 1])208 assert_equal(mbase.c._data,209 [b'5', b'5', b'three', b'four', b'five'])210 assert_equal(mbase.b._mask, [0, 0, 0, 0, 1])211 mbase = base.view(mrecarray).copy()212 mbase[:2] = masked213 assert_equal(mbase.a._data, [1, 2, 3, 4, 5])214 assert_equal(mbase.a._mask, [1, 1, 0, 0, 1])215 assert_equal(mbase.b._data, [1.1, 2.2, 3.3, 4.4, 5.5])216 assert_equal(mbase.b._mask, [1, 1, 0, 0, 1])217 assert_equal(mbase.c._data,218 [b'one', b'two', b'three', b'four', b'five'])219 assert_equal(mbase.b._mask, [1, 1, 0, 0, 1])220 def test_setslices_hardmask(self):221 # Tests setting slices w/ hardmask.222 base = self.base.copy()223 mbase = base.view(mrecarray)224 mbase.harden_mask()225 try:226 mbase[-2:] = (5, 5, 5)227 assert_equal(mbase.a._data, [1, 2, 3, 5, 5])228 assert_equal(mbase.b._data, [1.1, 2.2, 3.3, 5, 5.5])229 assert_equal(mbase.c._data,230 [b'one', b'two', b'three', b'5', b'five'])231 assert_equal(mbase.a._mask, [0, 1, 0, 0, 1])232 assert_equal(mbase.b._mask, mbase.a._mask)233 assert_equal(mbase.b._mask, mbase.c._mask)234 except NotImplementedError:235 # OK, not implemented yet...236 pass237 except AssertionError:238 raise239 else:240 raise Exception("Flexible hard masks should be supported !")241 # Not using a tuple should crash242 try:243 mbase[-2:] = 3244 except (NotImplementedError, TypeError):245 pass246 else:247 raise TypeError("Should have expected a readable buffer object!")248 def test_hardmask(self):249 # Test hardmask250 base = self.base.copy()251 mbase = base.view(mrecarray)252 mbase.harden_mask()253 assert_(mbase._hardmask)254 mbase.mask = nomask255 assert_equal_records(mbase._mask, base._mask)256 mbase.soften_mask()257 assert_(not mbase._hardmask)258 mbase.mask = nomask259 # So, the mask of a field is no longer set to nomask...260 assert_equal_records(mbase._mask,261 ma.make_mask_none(base.shape, base.dtype))262 assert_(ma.make_mask(mbase['b']._mask) is nomask)263 assert_equal(mbase['a']._mask, mbase['b']._mask)264 def test_pickling(self):265 # Test pickling266 base = self.base.copy()267 mrec = base.view(mrecarray)268 for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):269 _ = pickle.dumps(mrec, protocol=proto)270 mrec_ = pickle.loads(_)271 assert_equal(mrec_.dtype, mrec.dtype)272 assert_equal_records(mrec_._data, mrec._data)273 assert_equal(mrec_._mask, mrec._mask)274 assert_equal_records(mrec_._mask, mrec._mask)275 def test_filled(self):276 # Test filling the array...

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