How to use _create_dataframe method in pandera

Best Python code snippet using pandera_python

test_data.py

Source:test_data.py Github

copy

Full Screen

2import pytest3import pandas as pd4from ..data import (limit_rows, MaxRowsError, sample, pipe, to_values,5 to_json, to_csv)6def _create_dataframe(N):7 data = pd.DataFrame({"x": range(N), "y": range(N)})8 return data9def _create_data_with_values(N):10 data = {'values': [{'x': i, 'y': i+1} for i in range(N)]}11 return data12def test_limit_rows():13 """Test the limit_rows data transformer."""14 data = _create_dataframe(10)15 result = limit_rows(data, max_rows=20)16 assert data is result17 with pytest.raises(MaxRowsError):18 pipe(data, limit_rows(max_rows=5))19 data = _create_data_with_values(10)20 result = pipe(data, limit_rows(max_rows=20))21 assert data is result22 with pytest.raises(MaxRowsError):23 limit_rows(data, max_rows=5)24def test_sample():25 """Test the sample data transformer."""26 data = _create_dataframe(20)27 result = pipe(data, sample(n=10))28 assert len(result)==1029 assert isinstance(result, pd.DataFrame)30 data = _create_data_with_values(20)31 result = sample(data, n=10)32 assert isinstance(result, dict)33 assert 'values' in result34 assert len(result['values'])==1035 data = _create_dataframe(20)36 result = pipe(data, sample(frac=0.5))37 assert len(result)==1038 assert isinstance(result, pd.DataFrame)39 data = _create_data_with_values(20)40 result = sample(data, frac=0.5)41 assert isinstance(result, dict)42 assert 'values' in result43 assert len(result['values'])==1044def test_to_values():45 """Test the to_values data transformer."""46 data = _create_dataframe(10)47 result = pipe(data, to_values)48 assert result=={'values': data.to_dict(orient='records')}49def test_type_error():50 """Ensure that TypeError is raised for types other than dict/DataFrame."""51 for f in (sample, limit_rows, to_values):52 with pytest.raises(TypeError):53 pipe(0, f)54def test_dataframe_to_json():55 """Test to_json56 - make certain the filename is deterministic57 - make certain the file contents match the data58 """59 data = _create_dataframe(10)60 try:61 result1 = pipe(data, to_json)62 result2 = pipe(data, to_json)63 filename = result1['url']64 output = pd.read_json(filename)65 finally:66 os.remove(filename)67 assert result1 == result268 assert output.equals(data)69def test_dict_to_json():70 """Test to_json71 - make certain the filename is deterministic72 - make certain the file contents match the data73 """74 data = _create_data_with_values(10)75 try:76 result1 = pipe(data, to_json)77 result2 = pipe(data, to_json)78 filename = result1['url']79 output = pd.read_json(filename).to_dict(orient='records')80 finally:81 os.remove(filename)82 assert result1 == result283 assert data == {'values': output}84def test_dataframe_to_csv():85 """Test to_csv with dataframe input86 - make certain the filename is deterministic87 - make certain the file contents match the data88 """89 data = _create_dataframe(10)90 try:91 result1 = pipe(data, to_csv)92 result2 = pipe(data, to_csv)93 filename = result1['url']94 output = pd.read_csv(filename)95 finally:96 os.remove(filename)97 assert result1 == result298 assert output.equals(data)99def test_dict_to_csv():100 """Test to_csv with dict input101 - make certain the filename is deterministic102 - make certain the file contents match the data103 """...

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