feature_engine icon indicating copy to clipboard operation
feature_engine copied to clipboard

include transformer for datetime variables

Open solegalli opened this issue 6 years ago • 16 comments

The first version of this module should include the following:

  • New module: datetime (folder)

Three transformers:

  • ExtractDateFeatures
  • ExtractTimeFeatures
  • ExtractDateTimeFeatures

A base class:

  • DateTimeBaseTransformer

The base class should:

  • check that the variables entered by the user are datetime, or if string / object, transform them to datetime (I wonder if we should make this a function part of variable_manipulation? and call it in this transfomrer? In case we develop the timeseries module, we may need it later for other modules as well.

  • have a method to return time features

  • have a method to return date features

  • methods to check the input dataframe, and options on what to do if outliers are present

The ExtractDateFeatures should derive the following features:

  • [ ] month, quarter, semester and year (all numeric outputs)
  • [ ] week of the year
  • [ ] is week of the month supported by pandas? if yes, when we should return it
  • [ ] day (numeric 1-31), day of the week (numeric 0-6), and is_weekend (binary)
  • [ ] anything else supported by pandas?
  • [ ] anything else that would be useful

The ExtractTimeFeatures should extract the following features

  • [ ] hr, minute, second
  • [ ] timezone: with parameter return_timezone=False by default, we allow the user to return a time zone categorical feature
  • [ ] unify_timezone=False, to handle different timezones. If True, the transformer is timezone aware, unifies to greenwich and then derived features (should we give option to user to unify to something else? probably yes)
  • [ ] to discuss: is_morning, is_afternoon, is_evening
  • [ ] to discuss: working hrs (I am thinking of passing a string parameter like '9-17' and use those to determine this feature
  • [ ] anything else that would be useful

The ExtractDateTimeFeatures is a sum of the previous transformers, so it should return all possible date and time features.

The reason I suggest to break this into 3 classes is because some timestamps contain only dates, some only times and some both. And I think it would be easier if the user, who knows the timestamp selects the appropriate transformer, instead of adding code to understand which type of timestamp it is, and then derive the features

To consider: should we in version 1 of this transformer return all possible features? or should we give the user an option of which features to return? example, the user may want year and month but not quarter and semester.

Example code in recipes 2 to 5 of the this link..

Things to think about for the transformers design:

  • [ ] Transformer returns all new variables by default, or only those indicated by the user. This behavior could regulated by a parameter in the init. As per previous question, should we leave this for version 2 of this transformer? or add it straightaway?
  • [ ] Transformer should be able to derive features from more than 1 datetime variable at the time, like all feature engine transformers.
  • [ ] Option in Transformer to drops original datetime variables (as those are not useful for machine learning), drop_datetime_variables=True or similar.

Files needed in addition to code files:

  • [ ] add transformer to readme list
  • [ ] add transformer to docs/index.rst
  • [ ] add docs folder exclusive for this transformer with rst files with examples
  • [ ] add jupyter notebooks showing how to use this transformers

This issue can be done in 1 big PR, or multiple PRs, maybe 1 per transformer.

solegalli avatar Jul 10 '20 16:07 solegalli

Hello @solegalli, I'm assuming the requirement is for a transformer that parses a date field and decomposes it into a bunch of separate variables (numerics or characters). Do you have anything specific in mind for this if not I can get started on this issue.

KarthikKothareddy avatar Aug 09 '20 14:08 KarthikKothareddy

Made this comment part of the description at the top

I am thinking that on a transformer that would be able to return all the variables contained in recipes 2 to 5 of the above link. Those are:

  • [ ] year, month, quarter and semester

  • [ ] day (numeric 1-31) and day of the week (numeric 0-6), is_weekend (binary)

  • [ ] hr, minute, second

  • [ ] to discuss: is_morning, is_afternoon, is_evening

  • [ ] Transformer returns all by default, or only those indicated by the user.

  • [ ] Transformer allows user to indicate for which variables these variables should be returned.

  • [ ] Transformer drops original datetime variables (as those are not useful for machine learning), or should we add an argument to let the user decide? in this case, the default should be to drop.

  • [ ] the transformer can take format datetime or strings, in which case it would first parse it to datetime

We also need to make the new transformer part of the main index in the docs, and add a folder with a specific rst. We can discuss this later.

solegalli avatar Aug 09 '20 19:08 solegalli

@KarthikKothareddy small request, in your PR, please include only changes for this issue. Thank you!

solegalli avatar Aug 09 '20 19:08 solegalli

@solegalli Right on, I'll get started on this.

KarthikKothareddy avatar Aug 10 '20 13:08 KarthikKothareddy

@pradumna123 this one is also still pending in case you are interested :)

solegalli avatar Jan 06 '21 23:01 solegalli

Sounds good can work on this one.

pradumna123 avatar Jan 06 '21 23:01 pradumna123

The transformer accepts a datetime variable from dataframe and will create additional columns for days of week , month etc. And if the user enters 5 datetime and selects a combination of 4 new variables like week day month we will return a dataframe with 20 additional columns right?

pradumna123 avatar Jan 10 '21 22:01 pradumna123

yes, that is correct. There is a PR #111 for this issue already, but the code is a bit old. Not sure you want to start from scratch or use this as a base?

Maybe browse at my comments there?

thanks a lot

solegalli avatar Jan 11 '21 10:01 solegalli

@TremaMiguel this transformer is top priority. Would you like to give it a go?

There is a stale PR, but I think we should start afresh. The package has evolved a lot since then.

solegalli avatar Jul 18 '21 08:07 solegalli

@solegalli going to take a look at this, it would be better to elaborate a development plan on a google sheet, what functionalities do we want exactly ?, which are the ones to start?

TremaMiguel avatar Jul 20 '21 11:07 TremaMiguel

Yep, happy to work on a sheet. The functionality wanted is described at the top of the issue. Link the sheet to the issue when you have it ready if you want :)

solegalli avatar Jul 21 '21 11:07 solegalli

Hi, I recently took some of your courses on udemy and saw your post on linkedin. Take it this still needs some work? Gonna give it a go as my first issue if you don't mind, think it's good practice.

dodoarg avatar Nov 29 '21 20:11 dodoarg

Hi @dodoarg

That would be amazing. We welcome all the help we can get :)

solegalli avatar Nov 29 '21 22:11 solegalli

question @ChristopherGS and @NicoGalli

We want to introduce new functionality to extract temporal features from a datetime variable.

datetime variables can have dates, times or both.

the new functionality wants to extract date derived features from the date part if there is one (month, day, etc) and time (hr, min) features from the time part (if it exists).

My original idea was to have 3 transformers: 1 to derive features from date and time, 1 to derive features from time only and 1 to derive features from dates only.

Now I wonder if it would be best to have just 1 class, with a parameter to regulate the behaviour instead, for example, the parameter extract_features that can take values 'time', 'date' or 'both' for example.

Originally I thought of 3 different transformers to encourage users to think what variables they have, instead of just using a transformer and deriving all features even for example time, when their variables do not have a time component.

But now I am thinking that it introduces a lot of boilerplate and harder to maintain in the long run. So I am now inclined to go for 1 class only.

What would you do?

solegalli avatar Dec 02 '21 10:12 solegalli

@solegalli initially, working with dates and times are always a challenge as per de amount of formats, timezones, calendars, dailylight saving time and lot of ETCs. After have said that and taking into account that Datetime, Time and Date are 3 differents classes from which you will derive different new features, this may be a good case to keep it separated! I would recommend start with the Datetime one and discuss on this once again that one is finished, as that will give you a better picture about what to do next.

NicoGalli avatar Dec 03 '21 01:12 NicoGalli

@dodoarg as per Nico's suggestion let's begin by creating the DateTime class that derives all features, and then we see if we need to break it down.

solegalli avatar Dec 03 '21 07:12 solegalli