ENH ability to prefix and suffix columns

This commit is contained in:
Kevin Johnson
2020-05-20 15:40:26 -07:00
parent e1fecb7b72
commit df7680fae5
4 changed files with 45 additions and 6 deletions
+14 -1
View File
@@ -18,6 +18,7 @@ All the indicators return a named Series or a DataFrame in uppercase underscore
* Example Jupyter Notebook under the examples directory.
* Abbreviated Indicator names as listed below.
* *Extended Pandas DataFrame* as 'ta'. See examples below.
* Easily add prefixes or suffixes or both to columns names.
* Categories similar to [TA-lib](https://github.com/mrjbq7/ta-lib/tree/master/docs/func_groups).
@@ -41,7 +42,7 @@ All the indicators return a named Series or a DataFrame in uppercase underscore
- __Aberration__ (aberration)
- __BRAR__ (brar)
### What is a Pandas DataFrame Extension?
## What is a Pandas DataFrame Extension?
A [Pandas DataFrame Extension](https://pandas.pydata.org/pandas-docs/stable/extending.html), extends a DataFrame allowing one to add more functionality and features to Pandas to suit your needs. As such, it is now easier to run Technical Analysis on existing Financial Time Series without leaving the current DataFrame. This extension by default returns the Indicator result or it can append the result to the existing DataFrame by including the parameter 'append=True' in the method call. Examples below.
@@ -101,6 +102,18 @@ help(ta.log_return)
help(pd.DataFrame().ta.log_return)
```
## New DataFrame kwargs: *prefix* and *suffix*
```python
prehl2 = df.ta.hl2(prefix="pre")
print(prehl2.columns) # "pre_HL2"
endhl2 = df.ta.hl2(suffix="end")
print(endhl2.columns) # "HL2_end"
bothhl2 = df.ta.hl2(suffix="end")
print(bothhl2.columns) # "pre_HL2_end"
```
## New DataFrame Properties: *reverse* & *datetime_ordered*
+4 -4
View File
@@ -169,12 +169,12 @@ class AnalysisIndicators(BasePandasObject):
"""Add prefix and/or suffix to the result columns"""
if result is None: return
else:
prefix = ''
prefix = suffix = ''
if 'prefix' in kwargs:
prefix = kwargs['prefix'] + '_'
suffix = ''
prefix = f"{kwargs['prefix']}_"
if 'suffix' in kwargs:
suffix = '_' + kwargs['suffix']
suffix = f"_{kwargs['suffix']}"
if isinstance(result, pd.Series):
result.name = prefix + result.name + suffix
+1 -1
View File
@@ -6,7 +6,7 @@ long_description = "An easy to use Python 3 Pandas Extension with 95+ Technical
setup(
name ="pandas_ta",
packages =['pandas_ta', 'pandas_ta.momentum', 'pandas_ta.overlap', 'pandas_ta.performance', 'pandas_ta.statistics', 'pandas_ta.trend', 'pandas_ta.volatility', 'pandas_ta.volume'],
version ="0.1.47b",
version ="0.1.48b",
description =long_description,
long_description =long_description,
author ="Kevin Johnson",
+26
View File
@@ -1,3 +1,4 @@
from .config import sample_data
from .context import pandas_ta
from unittest import TestCase
@@ -16,6 +17,14 @@ data = {
}
class TestUtilities(TestCase):
@classmethod
def setUpClass(cls):
cls.data = sample_data
@classmethod
def tearDownClass(cls):
del cls.data
def setUp(self):
self.crosseddf = DataFrame(data)
self.utils = pandas_ta.utils
@@ -24,6 +33,23 @@ class TestUtilities(TestCase):
del self.crosseddf
del self.utils
def test__add_prefix_suffix(self):
result = self.data.ta.hl2(append=False, prefix="pre")
self.assertEqual(result.name, 'pre_HL2')
result = self.data.ta.hl2(append=False, suffix="suf")
self.assertEqual(result.name, 'HL2_suf')
result = self.data.ta.hl2(append=False, prefix="pre", suffix="suf")
self.assertEqual(result.name, 'pre_HL2_suf')
result = self.data.ta.hl2(append=False, prefix=1, suffix=2)
self.assertEqual(result.name, '1_HL2_2')
result = self.data.ta.macd(append=False, prefix="pre", suffix="suf")
for col in result.columns:
self.assertTrue(col.startswith('pre_') and col.endswith('_suf'))
def test__above_below(self):
result = self.utils._above_below(self.crosseddf['a'], self.crosseddf['zero'], above=True)
self.assertIsInstance(result, Series)