MCPcopy Index your code
hub / github.com/datacamp/pythonwhat / check_df

Function check_df

pythonwhat/checks/check_object.py:251–300  ·  view source on GitHub ↗

Check whether a DataFrame was defined and it is the right type ``check_df()`` is a combo of ``check_object()`` and ``is_instance()`` that checks whether the specified object exists and whether the specified object is pandas DataFrame. You can continue checking the data frame with ``che

(state, index, missing_msg=None, not_instance_msg=None, expand_msg=None)

Source from the content-addressed store, hash-verified

249
250
251def check_df(state, index, missing_msg=None, not_instance_msg=None, expand_msg=None):
252 """Check whether a DataFrame was defined and it is the right type
253
254 ``check_df()`` is a combo of ``check_object()`` and ``is_instance()`` that checks whether the specified object exists
255 and whether the specified object is pandas DataFrame.
256
257 You can continue checking the data frame with ``check_keys()`` function to 'zoom in' on a particular column in the pandas DataFrame:
258
259 Args:
260 index (str): Name of the data frame to zoom in on.
261 missing_msg (str): See ``check_object()``.
262 not_instance_msg (str): See ``is_instance()``.
263 expand_msg (str): If specified, this overrides any messages that are prepended by previous SCT chains.
264
265 :Example:
266
267 Suppose you want the student to create a DataFrame ``my_df`` with two columns.
268 The column ``a`` should contain the numbers 1 to 3,
269 while the contents of column ``b`` can be anything: ::
270
271 import pandas as pd
272 my_df = pd.DataFrame({"a": [1, 2, 3], "b": ["a", "n", "y"]})
273
274 The following SCT would robustly check that: ::
275
276 Ex().check_df("my_df").multi(
277 check_keys("a").has_equal_value(),
278 check_keys("b")
279 )
280
281 - ``check_df()`` checks if ``my_df`` exists (``check_object()`` behind the scenes) and is a DataFrame (``is_instance()``)
282 - ``check_keys("a")`` zooms in on the column ``a`` of the data frame, and ``has_equal_value()`` checks if the columns correspond between student and solution process.
283 - ``check_keys("b")`` zooms in on hte column ``b`` of the data frame, but there's no 'equality checking' happening
284
285 The following submissions would pass the SCT above: ::
286
287 my_df = pd.DataFrame({"a": [1, 1 + 1, 3], "b": ["a", "l", "l"]})
288 my_df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})
289
290 """
291 import pandas as pd
292 child = check_object(
293 state,
294 index,
295 missing_msg=missing_msg,
296 expand_msg=expand_msg,
297 typestr="pandas DataFrame",
298 )
299 is_instance(child, pd.DataFrame, not_instance_msg=not_instance_msg)
300 return child
301
302
303def check_keys(state, key, missing_msg=None, expand_msg=None):

Callers 1

test_data_frameFunction · 0.90

Calls 2

check_objectFunction · 0.85
is_instanceFunction · 0.85

Tested by 1

test_data_frameFunction · 0.72