Python practice · 15 questions

NumPy and pandas: practice set

15 questions on the NumPy and pandas behaviour that causes real bugs: dtypes and overflow, axes, views and copies, loc and iloc, missing data, groupby and joins. Every "what does this print" answer was checked by running the code with NumPy 2 and pandas 2. At the end you get a score per topic.

  1. Question 1 of 15 · NumPy arrays & dtypesWhat does this print?
    Python
    import numpy as np
    
    print(np.array([1, 2.5, 3]).dtype)
  2. Question 2 of 15 · NumPy arrays & dtypesWhat does this print?
    Python
    import numpy as np
    
    a = np.array([200], dtype=np.uint8)
    print((a + a)[0])
  3. Question 3 of 15 · NumPy indexingWhat does this print?
    Python
    import numpy as np
    
    a = np.array([5, -1, 3, -7])
    a[a < 0] = 0
    print(a.tolist())
  4. Question 4 of 15 · NumPy indexingWhat does this print?
    Python
    import numpy as np
    
    a = np.array([5, 6, 7])
    b = a[[0, 1]]
    b[0] = 99
    c = a[0:2]
    c[1] = 42
    print(a.tolist())
  5. Question 5 of 15 · NumPy computationWhat does this print?
    Python
    import numpy as np
    
    a = np.arange(6).reshape(2, 3)
    print(a.sum(axis=0).tolist(), a.sum(axis=1).tolist())
  6. Question 6 of 15 · NumPy computationWhat does this print?
    Python
    import numpy as np
    
    x = np.array([1, 5, 10])
    print(np.where(x > 4, 'big', 'small').tolist())
  7. Question 7 of 15 · pandas selectionWhat does this print?
    Python
    import pandas as pd
    
    s = pd.Series([10, 20, 30], index=[2, 0, 1])
    print(s.loc[0], s.iloc[0])
  8. Question 8 of 15 · pandas selectionWhat does this print?
    Python
    import pandas as pd
    
    df = pd.DataFrame({'v': range(5)})
    print(len(df.loc[1:3]), len(df.iloc[1:3]))
  9. Question 9 of 15 · Missing dataWhat does this print?
    Python
    import pandas as pd
    
    print(pd.Series(['a', None, 'a', 'b']).value_counts().to_dict())
  10. Question 10 of 15 · Missing dataWhat does this print?
    Python
    import pandas as pd
    
    df = pd.DataFrame({'a': [1, None, 3], 'b': [None, None, 6]})
    print(len(df.dropna()), df['b'].fillna(0).sum())
  11. Question 11 of 15 · Missing dataWhat does this print?
    Python
    import pandas as pd
    
    print(pd.Series([1, 2, None]).dtype, pd.Series([1, 2, None], dtype="Int64").dtype)
  12. Question 12 of 15 · Groupby & joinsWhat does this print?
    Python
    import pandas as pd
    
    df = pd.DataFrame({'k': ['a', 'a', 'b'], 'v': [1, 3, 5]})
    df['share'] = df['v'] / df.groupby('k')['v'].transform('sum')
    print(df['share'].tolist())
  13. Question 13 of 15 · Groupby & joinsWhat does this print?
    Python
    import pandas as pd
    
    left = pd.DataFrame({'id': [1, 2, 3]})
    right = pd.DataFrame({'id': [1, 3], 'name': ['x', 'z']})
    m = left.merge(right, on='id', how='left')
    print(len(m), m['name'].isna().sum())
  14. Question 14 of 15 · Time series & best practiceWhat does this print?
    Python
    import pandas as pd
    
    idx = pd.to_datetime(['2026-01-01 09:00', '2026-01-01 18:00', '2026-01-03 10:00'])
    s = pd.Series([1, 2, 3], index=idx)
    print(s.resample('D').sum().tolist())
  15. Question 15 of 15 · Time series & best practiceYou need to set status to "refund" for rows where amount < 0. Which line works reliably in every pandas version?
0 of 15 answered

Revise the topics in the Python for Data Engineers guide and Python guide.