In this guide, we will walk through an example of creating a Woodwork DataTable, and will show how to update and remove logical types and semantic tags. We will also demonstrate how to use the typing information to select subsets of data.
Woodwork relies heavily on the concepts of physical types, logical types and semantic tags. These concepts are covered in detail in Understanding Types and Tags, but brief definitions of each are provided here for reference:
Physical Type: defines how the data is stored on disk or in memory
Logical Type: defines how the data should be parsed or interpreted
Semantic Tag(s): provides additional data about the meaning of the data or how it should be used
Let’s demonstrate how to use Woodwork, starting off by creating a dataframe containing retail sales data.
[1]:
import woodwork as ww data = ww.demo.load_retail(nrows=100, return_dataframe=True) data.head(5)
As we can see, this is a dataframe containing several different data types, including dates, categorical values, numeric values and natural language descriptions. Let’s use Woodwork to create a DataTable from this data.
Creating a Woodwork DataTable is as simple as passing in a dataframe with the data of interest during initialization. An optional name parameter can be specified to label the DataTable.
[2]:
dt = ww.DataTable(data, name="retail") dt.types
Using just this simple call, Woodwork was able to infer the logical types present in our data by analyzing the dataframe dtypes as well as the information contained in the columns. In addition, Woodwork also added semantic tags to some of the columns based on the logical types that were inferred.
If the initial inference was not to our liking, the logical type can be changed to a more appropriate value. Let’s change some of the columns to a different logical type to illustrate this process. Below we will set the logical type for the quantity, customer_name and country columns to be Categorical.
quantity
customer_name
country
Categorical
[3]:
dt = dt.set_logical_types({ 'quantity': 'Categorical', 'customer_name': 'Categorical', 'country': 'Categorical' }) dt.types
If we now inspect the information in the types output, we can see that the Logical type for the three columns has been updated with the Categorical logical type we specified.
types
Now that we have logical types we are happy with, we can select a subset of the columns based on their logical types. Let’s select only the columns that have a logical type of WholeNumber or Double:
WholeNumber
Double
[4]:
numeric_dt = dt.select_ltypes(['WholeNumber', 'Double']) numeric_dt.types
This selection process has returned a new DataTable containing only the columns that match the logical types we specified. After we have selected the columns we want, we can also access a dataframe containing just those columns if we need it for additional analysis.
DataTable
[5]:
numeric_dt.to_pandas()
100 rows × 4 columns
Note
Accessing the dataframe associated with a DataTable by using dt.to_pandas() will return a reference to the dataframe. Modifications to the returned dataframe can cause unexpected results. If you need to modify the dataframe, you should use dt.to_pandas(copy=True) to return a copy of the stored dataframe that can be safely modified without impacting the DataTable behavior.
dt.to_pandas()
dt.to_pandas(copy=True)
Next, let’s add semantic tags to some of the columns. We will add the tag of product_details to the description column and tag the total column with currency.
product_details
description
total
currency
[6]:
dt = dt.set_semantic_tags({'description':'product_details', 'total': 'currency'}) dt.types
We can also select columns based on a semantic tag. Perhaps we want to only select the columns tagged with category:
category
[7]:
category_dt = dt.select_semantic_tags('category') category_dt.types
We can also select columns using mutiple semantic tags, or even a mixture of semantic tags and logical types:
[8]:
category_numeric_dt = dt.select_semantic_tags(['numeric', 'category']) category_numeric_dt.types
[9]:
mixed_dt = dt.select(['Boolean', 'product_details']) mixed_dt.types
If we wanted to select an individual column, we just need to specify the column name. We can then get access to the data in the DataColumn using the to_pandas method:
to_pandas
[10]:
dc = dt['total'] dc
<DataColumn: total (Physical Type = float64) (Logical Type = Double) (Semantic Tags = {'numeric', 'currency'})>
[11]:
dc.to_pandas()
0 25.245 1 33.561 2 36.300 3 33.561 4 33.561 ... 95 25.245 96 83.160 97 21.780 98 21.780 99 21.780 Name: total, Length: 100, dtype: float64
You can also access multiple columns by supplying a list of column names:
[12]:
multiple_cols_dt = dt[['product_id', 'total', 'unit_price']] multiple_cols_dt.types
We can also remove specific semantic tags from a column if they are no longer needed. Let’s remove the product_details tag from the description column:
[13]:
dt = dt.remove_semantic_tags({'description':'product_details'}) dt.types
Notice how the product_details tag has now been removed from the description column. If we wanted to remove all user-added semantic tags from all columns, we can also do that:
[14]:
dt = dt.reset_semantic_tags() dt.types
At any point, we can designate certain columns as the DataTable’s index and with the methods set_index and set_time_index. These methods can be used to assign these columns for the first time or to change the column being used as the index or time index.
index
Index and time index columns contain index and time_index semantic tags, respectively.
time_index
[15]:
dt = dt.set_index('order_product_id') dt.index
'order_product_id'
[16]:
dt = dt.set_time_index('order_date') dt.time_index
'order_date'
[17]:
dt.types
We can also retrieve all the Logical Types present in Woodwork. These can be useful for understanding the Logical Types, and how they will be interpreted.
[18]:
from woodwork.utils import list_logical_types list_logical_types()