question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

_get_column() change proposal

See original GitHub issue

Hi Kevin,

I noticed some behavior which seemed confusing, at least to me. While using df.ta.hlc3(append=True), I noticed that the column that was created was exactly the ‘close’ column (mind that I have set df.ta.adjusted = close). Stepping through the code uncovered that this is because I didn’t give explicit ‘high’, ‘low’ and ‘close’ columns, and if those are left to None, _get_column() will return ‘adjusted’ (‘close’ in my case) unless that’s not set, in which case it will return default which is passed as an argument.

line 388-389 in core.py

    elif series is None or default is None:
        return df[self.adjusted] if self.adjusted is not None else df[default]

What I found strange about this implementation is that even if a default is given (‘high’, ‘low’, ‘close’ respectively for hlc3), the function will still return the ‘adjusted’ column if it is set explicitely. To me that doesn’t really make sense with the ‘default’ name of the argument.

I propose to change the _get_column() function along these lines:

    def _get_column(self, series, default):

        """Attempts to get the correct series or 'column' and return it."""
        df = self._df
        if df is None: return

        # def _get_case(column: str):
        #     cases = [column.lower(), column.upper(), column.title()]
        #     return [c for i, c in enumerate(cases) if column == cases[i]].pop()
        # default = _get_case(default)

        # Explicitly passing a pd.Series to override default.
        if isinstance(series, pd.Series):
            return series
        **# Apply default if no series, or adjusted if no default is passed.
        elif series is None:
            if default is not None:
                series = default
            else:
                series = self.adjusted

        # Try to interpret passed string as a column.
        if isinstance(series, str):**
            # Return the df column since it's in there.
            if series in df.columns:
                return df[series]
            else:
                # Attempt to match the 'series' because it was likely misspelled.
                matches = df.columns.str.match(series, case=False)
                match = [i for i, x in enumerate(matches) if x]
                # If found, awesome.  Return it or return the 'series'.
                cols = ', '.join(list(df.columns))
                NOT_FOUND = f"[X] Ooops!!!: It's {series not in df.columns}, the series '{series}' was not found in {cols}"
                return df.iloc[:,match[0]] if len(match) else print(NOT_FOUND)

This way the passed ‘default’ column gets priority over the ‘adjusted’ column. Moving the ‘if isinstance(series,str)’ to a separate ‘if’ statement also allows for slightly misspelled default column names to work as well.

Since this is a core function I wanted to first open a discussion before sending a PR as it may potentially break things that I’m not aware of, or there may be a particular reason why this has been implemented as it is right now. If that’s the case, I’m happy to hear the reasoning as it may help me pay attention to usage of the _get_column() function in the future.

EDIT: cleaned up code markup

Best regards, Wout

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
WoutGeeurickxcommented, Sep 29, 2020

Hi @DrPaprikaa ,

You are absolutely correct! I’m less experienced with the usage of kwargs, so I wasn’t aware that the second parameter in kwargs.pop() is the fallback in case the key is not found. This works exactly as what I tried to propose, and in a clean way (although I like the explicit method definition ‘of old’). I’m sorry for raising this issue since it seems that everything would just have been solved if I had updated to the latest version. I still find it strange because I made sure to look up that piece of the source code on what I believed what was the latest version.

@twopirllc , I believe this issue can be closed, unless of course you want to keep this as a discussion thread for explicit method definition?

Wout

1reaction
DrPaprikaacommented, Sep 29, 2020

Hi, If I’m not mistaken this bug is not happening in the current master version.

high = self._get_column(kwargs.pop("high", "high"))

here, pop searches for the value associated with the key high in kwargs (chained indicator), defaulting with the value high (normal indicators). So _get_column() is never passed None. If None is passed for whatever reason, _get_column() simply returns adjusted, or None if not present via the else None that I added a few versions back in line 384 :


elif series is None:
    return df[self.adjusted] if self.adjusted is not None else None

So I think that nothing needs to be changed, correct me if I’m wrong 😃

DrPaprikaa

Read more comments on GitHub >

github_iconTop Results From Across the Web

getColumn - Studio Code.org
Retrieves the values stored in a given column of a data table and returns them in a list. You can only retrieve values...
Read more >
Power Automate SharePoint Get Column Changes - YouTube
PowerAutomate #SharePointWith the Get Changes Action in Power Automate we can determine if a column has changed in your SharePoint list or ...
Read more >
PowerAutomate - Get Column before & After value changes
This video explains how to get previous value of field changes in SharePoint item using Power Automate.Watch full video to understand how ...
Read more >
Power Automate SharePoint Get Column Changes
With the Get Changes Action in Power Automate we can determine if a column has changed in your SharePoint list or library.
Read more >
Google Spreadsheet SCRIPT Check if edited cell is in a ...
The issue is, I have a spreadsheet that I edit headers and text on and I do not want the update time in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found