Act fast, special offers end soon! Up to $294 is savings when you get Premium today.
Get offer codes

How do I know if a library method/function exists?

I was going through a lesson on matplotlib. One of the instructions was to convert the values to datetime type.

Before reading the instructions, I wouldn’t have known that pd.to_datetime() existed. I probably would have done a loop to convert it.

So it got me wondering, how does one even begin to know that the function/method exists?

Great question. There are different ways that can help you out -

  • You learn about them through such courses, especially if you are a beginner
  • You read someone else’s code (like through tutorials, blog posts, StackOverflow questions etc)
  • You simply google for it (like “convert to datetime in pandas”). This could also lead you to the 2nd point above.
  • You read the documentation (or at least take quick glances at it).
  • You look up cheatsheets for a library online. Usually, those are created by other, more experienced people and contain commonly used methods/functions.

It’s difficult, but also try to create a habit of questioning your code from time to time and asking if there could be a different or better way to do something. If you are working with a library, then there likely is a better way and you can try to find it then.

1 Like

Something I like to do a lot is to inspect dir(obj) for given objects obj.

>>> import pandas as pd
>>> dir(pd)
['BooleanDtype', 'Categorical', 'CategoricalDtype', 'CategoricalIndex', 'DataFrame', 'DateOffset', 'DatetimeIndex', 'DatetimeTZDtype', 'ExcelFile', 'ExcelWriter', 'Flags', 'Float32Dtype', 'Float64Dtype', 'Float64Index', 'Grouper', 'HDFStore', 'Index', 'IndexSlice', 'Int16Dtype', 'Int32Dtype', 'Int64Dtype', 'Int64Index', 'Int8Dtype', 'Interval', 'IntervalDtype', 'IntervalIndex', 'MultiIndex', 'NA', 'NaT', 'NamedAgg', 'Period', 'PeriodDtype', 'PeriodIndex', 'RangeIndex', 'Series', 'SparseDtype', 'StringDtype', 'Timedelta', 'TimedeltaIndex', 'Timestamp', 'UInt16Dtype', 'UInt32Dtype', 'UInt64Dtype', 'UInt64Index', 'UInt8Dtype', '__builtins__', '__cached__', '__doc__', '__docformat__', '__file__', '__getattr__', '__git_version__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_config', '_hashtable', '_is_numpy_dev', '_lib', '_libs', '_np_version_under1p17', '_np_version_under1p18', '_testing', '_tslib', '_typing', '_version', 'api', 'array', 'arrays', 'bdate_range', 'compat', 'concat', 'core', 'crosstab', 'cut', 'date_range', 'describe_option', 'errors', 'eval', 'factorize', 'get_dummies', 'get_option', 'infer_freq', 'interval_range', 'io', 'isna', 'isnull', 'json_normalize', 'lreshape', 'melt', 'merge', 'merge_asof', 'merge_ordered', 'notna', 'notnull', 'offsets', 'option_context', 'options', 'pandas', 'period_range', 'pivot', 'pivot_table', 'plotting', 'qcut', 'read_clipboard', 'read_csv', 'read_excel', 'read_feather', 'read_fwf', 'read_gbq', 'read_hdf', 'read_html', 'read_json', 'read_orc', 'read_parquet', 'read_pickle', 'read_sas', 'read_spss', 'read_sql', 'read_sql_query', 'read_sql_table', 'read_stata', 'read_table', 'reset_option', 'set_eng_float_format', 'set_option', 'show_versions', 'test', 'testing', 'timedelta_range', 'to_datetime', 'to_numeric', 'to_pickle', 'to_timedelta', 'tseries', 'unique', 'util', 'value_counts', 'wide_to_long']

Then I skim through the results and look for something that seems to relate to my needs or just inspect anything that catches my eye. For instance, just now I learned that pandas.read_clipboard is a thing.

So I inspect this particular object with help(pd.read_clipboard), which displays the following.

Help on function read_clipboard in module pandas.io.clipboards:

read_clipboard(sep='\\s+', **kwargs)
    Read text from clipboard and pass to read_csv.
    
    Parameters
    ----------
    sep : str, default '\s+'
        A string or regex delimiter. The default of '\s+' denotes
        one or more whitespace characters.
    
    **kwargs
        See read_csv for the full argument list.
    
    Returns
    -------
    DataFrame
        A parsed DataFrame object.

Pretty cool, it will probably come in handy sometimes.

2 Likes

Thanks! This is a really good idea!

DQ can consider including this in one of the lessons (or maybe I haven’t got there yet).

While the DQ courses teaches us how to fish. I think this takes it a step further, it teaches us how to make a fishing rod.

1 Like