Repository: pandas-dev/pandas

Issue:
Bug: pct_change with frequency set as 'BM' throws value error

#### Code Sample, a copy-pastable example if possible
```
import pandas as pd
import random
import numpy as np


Creating the time-series index 
n=60
index = pd.date_range('01/13/2020', periods = 70,freq='D') 
  
Creating the dataframe  
df = pd.DataFrame({"A":np.random.uniform(low=0.5, high=13.3, size=(70,)), 
                   "B":np.random.uniform(low=10.5, high=45.3, size=(70,)),  
                   "C":np.random.uniform(low=70.5, high=85, size=(70,)), 
                   "D":np.random.uniform(low=50.5, high=65.7, size=(70,))}, index = index) 


df.pct_change(freq='BM')

```
#### Problem description

For a time-series data, the df.pct_change(freq='BM') doesn't works and throws following error.

ValueError: cannot reindex from a duplicate axis

#### Expected Output
df.asfreq('BM').pct_change()

#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------
commit           : None
python           : 3.6.3.final.0
python-bits      : 64
OS               : Windows
OS-release       : 10
machine          : AMD64
processor        : Intel64 Family 6 Model 142 Stepping 9, GenuineIntel
byteorder        : little
LC_ALL           : None
LANG             : None
LOCALE           : None.None

pandas           : 0.25.1
numpy            : 1.15.4
pytz             : 2017.2
dateutil         : 2.6.1
pip              : 18.0
setuptools       : 39.1.0
Cython           : 0.26.1
pytest           : 5.1.2
hypothesis       : None
sphinx           : 1.6.3
blosc            : None
feather          : None
xlsxwriter       : 1.0.2
lxml.etree       : 4.1.0
html5lib         : 0.9999999
pymysql          : None
psycopg2         : 2.7.5 (dt dec pq3 ext lo64)
jinja2           : 2.9.6
IPython          : 6.1.0
pandas_datareader: None
bs4              : 4.6.0
bottleneck       : 1.2.1
fastparquet      : None
gcsfs            : None
lxml.etree       : 4.1.0
matplotlib       : 2.1.0
numexpr          : 2.6.2
odfpy            : None
openpyxl         : 2.4.8
pandas_gbq       : None
pyarrow          : 0.8.0
pytables         : None
s3fs             : None
scipy            : 1.0.0
sqlalchemy       : 1.1.13
tables           : 3.4.2
xarray           : 0.10.7
xlrd             : 1.1.0
xlwt             : 1.3.0
xlsxwriter       : 1.0.2

</details>



Discussion:
Hello, I have found that this happens when the offset which is made of `freq` is anchored. (`offset.isAnchored`)

so I propose that when the freq gets anchored, make the data resampled first, before the next line
`rs = data.div(data.shift(periods=periods, freq=freq, axis=axis, **kwargs)) - 1`

```diff
--- a/pandas/core/generic.py
+++ b/pandas/core/generic.py
@@ -10413,6 +10413,8 @@ class NDFrame(PandasObject, SelectionMixin):
         else:
             data = self.fillna(method=fill_method, limit=limit, axis=axis)

+        if freq and to_offset(freq).isAnchored():
+            data = data.asfreq(freq)
         rs = data.div(data.shift(periods=periods, freq=freq, axis=axis, **kwargs)) - 1
         rs = rs.reindex_like(data)
         if freq is None:
```
However, I couldn't run tests yet because, in the latest commit c4489c, it spewed out a lot of fails.
@0xF4D3C0D3 Can you change the label of this issue as Bug ?
No, I think I can't do that because I'm not a contributor nor member.

Resolving patch:
diff --git a/pandas/core/generic.py b/pandas/core/generic.py
--- a/pandas/core/generic.py
+++ b/pandas/core/generic.py
@@ -10443,6 +10443,7 @@ def pct_change(self, periods=1, fill_method="pad", limit=None, freq=None, **kwar
             data = self.fillna(method=fill_method, limit=limit, axis=axis)
 
         rs = data.div(data.shift(periods=periods, freq=freq, axis=axis, **kwargs)) - 1
+        rs = rs.loc[~rs.index.duplicated()]
         rs = rs.reindex_like(data)
         if freq is None:
             mask = isna(com.values_from_object(data))


Test patch:
diff --git a/pandas/tests/series/test_timeseries.py b/pandas/tests/series/test_timeseries.py
--- a/pandas/tests/series/test_timeseries.py
+++ b/pandas/tests/series/test_timeseries.py
@@ -370,6 +370,16 @@ def test_pct_change(self, datetime_series):
             rs, (filled / filled.shift(freq="5D") - 1).reindex_like(filled)
         )
 
+    def test_pct_change_with_duplicate_axis(self):
+        # GH 28664
+        common_idx = date_range("2019-11-14", periods=5, freq="D")
+        result = Series(range(5), common_idx).pct_change(freq="B")
+
+        # the reason that the expected should be like this is documented at PR 28681
+        expected = Series([np.NaN, np.inf, np.NaN, np.NaN, 3.0], common_idx)
+
+        tm.assert_series_equal(result, expected)
+
     def test_pct_change_shift_over_nas(self):
         s = Series([1.0, 1.5, np.nan, 2.5, 3.0])