<h4 dir="auto">Groupby Pct_change Bug Report</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from io import StringIO
import pandas as pd

txt = StringIO(&quot;&quot;&quot;Group;Date;Value
  A     ;01-02-2016     ;16 
  A     ;01-03-2016     ;15 
  A     ;01-04-2016     ;14 
  A     ;01-05-2016     ;17 
  A     ;01-06-2016     ;19 
  A     ;01-07-2016     ;20 
  B     ;01-02-2016     ;16 
  B     ;01-03-2016     ;13 
  B     ;01-04-2016     ;13 
  C     ;01-02-2016     ;16 
  C     ;01-03-2016     ;16 &quot;&quot;&quot;)

df = pd.read_table(txt,sep=';',parse_dates=[1])
d1 = df.set_index(['Date', 'Group']).Value
d2 = d1.groupby(level='Group').pct_change()
print(d2)"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">io</span> <span class="pl-k">import</span> <span class="pl-v">StringIO</span>
<span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>

<span class="pl-s1">txt</span> <span class="pl-c1">=</span> <span class="pl-v">StringIO</span>(<span class="pl-s">"""Group;Date;Value</span>
<span class="pl-s">  A     ;01-02-2016     ;16 </span>
<span class="pl-s">  A     ;01-03-2016     ;15 </span>
<span class="pl-s">  A     ;01-04-2016     ;14 </span>
<span class="pl-s">  A     ;01-05-2016     ;17 </span>
<span class="pl-s">  A     ;01-06-2016     ;19 </span>
<span class="pl-s">  A     ;01-07-2016     ;20 </span>
<span class="pl-s">  B     ;01-02-2016     ;16 </span>
<span class="pl-s">  B     ;01-03-2016     ;13 </span>
<span class="pl-s">  B     ;01-04-2016     ;13 </span>
<span class="pl-s">  C     ;01-02-2016     ;16 </span>
<span class="pl-s">  C     ;01-03-2016     ;16 """</span>)

<span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-en">read_table</span>(<span class="pl-s1">txt</span>,<span class="pl-s1">sep</span><span class="pl-c1">=</span><span class="pl-s">';'</span>,<span class="pl-s1">parse_dates</span><span class="pl-c1">=</span>[<span class="pl-c1">1</span>])
<span class="pl-s1">d1</span> <span class="pl-c1">=</span> <span class="pl-s1">df</span>.<span class="pl-en">set_index</span>([<span class="pl-s">'Date'</span>, <span class="pl-s">'Group'</span>]).<span class="pl-v">Value</span>
<span class="pl-s1">d2</span> <span class="pl-c1">=</span> <span class="pl-s1">d1</span>.<span class="pl-en">groupby</span>(<span class="pl-s1">level</span><span class="pl-c1">=</span><span class="pl-s">'Group'</span>).<span class="pl-en">pct_change</span>()
<span class="pl-en">print</span>(<span class="pl-s1">d2</span>)</pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">I tried to calculate value changes by group in pandas , according this stackoverflow answer, <a href="https://stackoverflow.com/questions/41453325/python-pandas-groupby-calculate-change" rel="nofollow">https://stackoverflow.com/questions/41453325/python-pandas-groupby-calculate-change</a></p>
<p dir="auto">the expected result should be</p>
<h4 dir="auto">Expected Output</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="Date        Group
2016-01-02  A             NaN
2016-01-03  A       -0.062500
2016-01-04  A       -0.066667
2016-01-05  A        0.214286
2016-01-06  A        0.117647
2016-01-07  A        0.052632
2016-01-02  B             NaN
2016-01-03  B       -0.187500
2016-01-04  B        0.000000
2016-01-02  C             NaN
2016-01-03  C        0.000000
Name: Value, dtype: float64"><pre class="notranslate"><span class="pl-v">Date</span>        <span class="pl-v">Group</span>
<span class="pl-c1">2016</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">02</span>  <span class="pl-v">A</span>             <span class="pl-v">NaN</span>
<span class="pl-c1">2016</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">03</span>  <span class="pl-v">A</span>       <span class="pl-c1">-</span><span class="pl-c1">0.062500</span>
<span class="pl-c1">2016</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">04</span>  <span class="pl-v">A</span>       <span class="pl-c1">-</span><span class="pl-c1">0.066667</span>
<span class="pl-c1">2016</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">05</span>  <span class="pl-v">A</span>        <span class="pl-c1">0.214286</span>
<span class="pl-c1">2016</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">06</span>  <span class="pl-v">A</span>        <span class="pl-c1">0.117647</span>
<span class="pl-c1">2016</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">07</span>  <span class="pl-v">A</span>        <span class="pl-c1">0.052632</span>
<span class="pl-c1">2016</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">02</span>  <span class="pl-v">B</span>             <span class="pl-v">NaN</span>
<span class="pl-c1">2016</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">03</span>  <span class="pl-v">B</span>       <span class="pl-c1">-</span><span class="pl-c1">0.187500</span>
<span class="pl-c1">2016</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">04</span>  <span class="pl-v">B</span>        <span class="pl-c1">0.000000</span>
<span class="pl-c1">2016</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">02</span>  <span class="pl-v">C</span>             <span class="pl-v">NaN</span>
<span class="pl-c1">2016</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">03</span>  <span class="pl-v">C</span>        <span class="pl-c1">0.000000</span>
<span class="pl-v">Name</span>: <span class="pl-v">Value</span>, <span class="pl-s1">dtype</span>: <span class="pl-s1">float64</span></pre></div>
<p dir="auto">First pecent change value of each group should be NaN, while in my computer with pandas version 0.23.1, the result shows as below.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="Date        Group   
2016-01-02    A              NaN
2016-01-03    A        -0.062500
2016-01-04    A        -0.066667
2016-01-05    A         0.214286
2016-01-06    A         0.117647
2016-01-07    A         0.052632
2016-01-02    B        -0.200000
2016-01-03    B        -0.187500
2016-01-04    B         0.000000
2016-01-02    C         0.230769
2016-01-03    C         0.000000
Name: Value, dtype: float64"><pre class="notranslate"><span class="pl-v">Date</span>        <span class="pl-v">Group</span>   
<span class="pl-c1">2016</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">02</span>    <span class="pl-v">A</span>              <span class="pl-v">NaN</span>
<span class="pl-c1">2016</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">03</span>    <span class="pl-v">A</span>        <span class="pl-c1">-</span><span class="pl-c1">0.062500</span>
<span class="pl-c1">2016</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">04</span>    <span class="pl-v">A</span>        <span class="pl-c1">-</span><span class="pl-c1">0.066667</span>
<span class="pl-c1">2016</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">05</span>    <span class="pl-v">A</span>         <span class="pl-c1">0.214286</span>
<span class="pl-c1">2016</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">06</span>    <span class="pl-v">A</span>         <span class="pl-c1">0.117647</span>
<span class="pl-c1">2016</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">07</span>    <span class="pl-v">A</span>         <span class="pl-c1">0.052632</span>
<span class="pl-c1">2016</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">02</span>    <span class="pl-v">B</span>        <span class="pl-c1">-</span><span class="pl-c1">0.200000</span>
<span class="pl-c1">2016</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">03</span>    <span class="pl-v">B</span>        <span class="pl-c1">-</span><span class="pl-c1">0.187500</span>
<span class="pl-c1">2016</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">04</span>    <span class="pl-v">B</span>         <span class="pl-c1">0.000000</span>
<span class="pl-c1">2016</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">02</span>    <span class="pl-v">C</span>         <span class="pl-c1">0.230769</span>
<span class="pl-c1">2016</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">03</span>    <span class="pl-v">C</span>         <span class="pl-c1">0.000000</span>
<span class="pl-v">Name</span>: <span class="pl-v">Value</span>, <span class="pl-s1">dtype</span>: <span class="pl-s1">float64</span></pre></div>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
<h2 dir="auto">INSTALLED VERSIONS</h2>
<p dir="auto">commit: None<br>
python: 3.6.5.final.0<br>
python-bits: 64<br>
OS: Windows<br>
OS-release: 10<br>
machine: AMD64<br>
processor: AMD64 Family 23 Model 1 Stepping 1, AuthenticAMD<br>
byteorder: little<br>
LC_ALL: None<br>
LANG: None<br>
LOCALE: None.None</p>
<p dir="auto">pandas: 0.23.1<br>
pytest: 3.5.1<br>
pip: 10.0.1<br>
setuptools: 39.1.0<br>
Cython: 0.28.2<br>
numpy: 1.14.3<br>
scipy: 1.1.0<br>
pyarrow: None<br>
xarray: None<br>
IPython: 6.4.0<br>
sphinx: 1.7.4<br>
patsy: 0.5.0<br>
dateutil: 2.7.3<br>
pytz: 2018.4<br>
blosc: None<br>
bottleneck: 1.2.1<br>
tables: 3.4.3<br>
numexpr: 2.6.5<br>
feather: None<br>
matplotlib: 2.2.2<br>
openpyxl: 2.5.3<br>
xlrd: 1.1.0<br>
xlwt: 1.3.0<br>
xlsxwriter: 1.0.4<br>
lxml: 4.2.1<br>
bs4: 4.6.0<br>
html5lib: 0.9999999<br>
sqlalchemy: 1.2.7<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: 2.10<br>
s3fs: None<br>
fastparquet: None<br>
pandas_gbq: None<br>
pandas_datareader: None</p>
</details>
<h4 dir="auto">Code Sample, a copy-pastable example if possible</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="# Your code here
df=pd.DataFrame([[1,2],[3,4]], columns=list('aa')) . # NOTICE TWO COLUMNS EACH CALLED 'a' 
df.drop_duplicates()"><pre class="notranslate"><span class="pl-c"># Your code here</span>
<span class="pl-s1">df</span><span class="pl-c1">=</span><span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>([[<span class="pl-c1">1</span>,<span class="pl-c1">2</span>],[<span class="pl-c1">3</span>,<span class="pl-c1">4</span>]], <span class="pl-s1">columns</span><span class="pl-c1">=</span><span class="pl-en">list</span>(<span class="pl-s">'aa'</span>)) . <span class="pl-c"># NOTICE TWO COLUMNS EACH CALLED 'a' </span>
<span class="pl-s1">df</span>.<span class="pl-en">drop_duplicates</span>()</pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">May be related to 10161</p>
<p dir="auto">Above code blows up with exception:</p>
<p dir="auto">[...]<br>
/opt/virtualenvs/luigi/local/lib/python2.7/dist-packages/pandas/core/frame.pyc in f(vals)<br>
3095             labels, shape = algos.factorize(vals,<br>
3096                                             size_hint=min(len(self),<br>
-&gt; 3097                                                           _SIZE_HINT_LIMIT))<br>
3098             return labels.astype('i8', copy=False), len(shape)<br>
3099</p>
<p dir="auto">/opt/virtualenvs/luigi/local/lib/python2.7/dist-packages/pandas/core/algorithms.pyc in factorize(values, sort, order, na_sentinel, size_hint)<br>
183     table = hash_klass(size_hint or len(vals))<br>
184     uniques = vec_klass()<br>
--&gt; 185     labels = table.get_labels(vals, uniques, 0, na_sentinel, True)<br>
186<br>
187     labels = com._ensure_platform_int(labels)</p>
<p dir="auto">pandas/hashtable.pyx in pandas.hashtable.Int64HashTable.get_labels (pandas/hashtable.c:7941)()</p>
<p dir="auto">ValueError: Buffer has wrong number of dimensions (expected 1, got 2)</p>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">I was excepting simple dataframe output as there are no duplicate rows there.</p>
<p dir="auto">a  a<br>
0  1  2<br>
1  3  4</p>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
<h2 dir="auto">INSTALLED VERSIONS</h2>
<p dir="auto">commit: None<br>
python: 2.7.12.final.0<br>
python-bits: 64<br>
OS: Linux<br>
OS-release: 4.9.32-15.41.amzn1.x86_64<br>
machine: x86_64<br>
processor: x86_64<br>
byteorder: little<br>
LC_ALL: None<br>
LANG: en_US.UTF-8</p>
<p dir="auto">pandas: 0.18.1<br>
nose: None<br>
pip: 9.0.1<br>
setuptools: 36.4.0<br>
Cython: None<br>
numpy: 1.11.0<br>
scipy: 0.17.1<br>
statsmodels: 0.8.0<br>
xarray: None<br>
IPython: 5.4.1<br>
sphinx: None<br>
patsy: 0.4.1<br>
dateutil: 2.6.1<br>
pytz: 2017.2<br>
blosc: None<br>
bottleneck: None<br>
tables: None<br>
numexpr: None<br>
matplotlib: 2.0.2<br>
openpyxl: None<br>
xlrd: None<br>
xlwt: None<br>
xlsxwriter: None<br>
lxml: None<br>
bs4: None<br>
html5lib: 0.999999999<br>
httplib2: None<br>
apiclient: None<br>
sqlalchemy: 1.1.14<br>
pymysql: 0.7.11.None<br>
psycopg2: None<br>
jinja2: 2.7.3<br>
boto: 2.48.0<br>
pandas_datareader: None</p>
</details>