Repository: JDASoftwareGroup/rle-array

Issue:
Bug with `mean` using your `README` demo
Hi there. Thanks for this interesting extension, I'm thinking of using it in an upcoming conference talk. I've spent most of this year talking on High Performance Python (I'm co-author of the O'Reilly publication with this name) and for an upcoming talk I'd like to include RLE as an example of compression. Past talks here if you're curious: https://ianozsvald.com/

I've used your `README` demo and I see that performance is often the same (or a little slower) for compressed data but on one test I've had a failure. `df.month.mean() != df_rle.month.mean()` as `df_rel.month.mean() -> np.inf` (which it shouldn't).

Specifically:
```
In [29]: df.month.mean()
Out[29]: 6.2505
In [30]: df_rle.month.mean() 
/home/ian/workspace/scratch/.../rle-array-master/rle_array/array.py:460: RuntimeWarning: divide by zero encountered in long_scalars   return np.dot(data, lengths) / self.dtype._dtype.type(n)
Out[30]: inf
```

Some other tests are fine:
```
df.month.sum() == df_rle.month.sum()  # True
df.month.std() == df_rle.month.std()  # True
```

Here's my DataFrame configuration:
```
In [50]: df_rle.month.value_counts()                                                                                                                                                                                                          
/home/ian/workspace/scratch/.../rle-array-master/rle_array/array.py:372: PerformanceWarning: performance: __array__ blows up data
  warnings.warn("performance: __array__ blows up data", PerformanceWarning)
Out[50]: 
1     372000
3     372000
5     372000
4     360000
6     344000
2     340000
7     310000
8     310000
10    310000
12    310000
9     300000
11    300000
Name: month, dtype: int64

In [52]: df_rle.info()                                                                                                                                                                                                                        
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4000000 entries, 0 to 3999999
Data columns (total 8 columns):
 #   Column    Dtype           
---  ------    -----           
 0   date      datetime64[ns]  
 1   month     RLEDtype[int8]  
 2   year      RLEDtype[int16] 
 3   city      RLEDtype[object]
 4   country   RLEDtype[object]
 5   avg_temp  float32         
 6   rain      RLEDtype[bool]  
 7   mood      RLEDtype[object]
dtypes: RLEDtype[bool](1), RLEDtype[int16](1), RLEDtype[int8](1), RLEDtype[object](3), datetime64[ns](1), float32(1)
memory usage: 69.6 MB
```

Version information:

```
In [22]: pd.show_versions()                                                                                                                                                                                                                   
/home/ian/miniconda3/envs/.../lib/python3.7/site-packages/setuptools/distutils_patch.py:26: UserWarning: Distutils was imported before Setuptools. This usage is discouraged and may exhibit undesirable behaviors or errors. Please use Setuptools' objects directly or at least import Setuptools first.
  "Distutils was imported before Setuptools. This usage is discouraged "

INSTALLED VERSIONS
------------------
commit           : None
python           : 3.7.8.final.0
python-bits      : 64
OS               : Linux
OS-release       : 5.6.7-050607-generic
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : None
LANG             : en_GB.UTF-8
LOCALE           : en_GB.UTF-8

pandas           : 1.0.5
numpy            : 1.19.1
pytz             : 2020.1
dateutil         : 2.8.1
pip              : 20.1.1
setuptools       : 49.2.0.post20200712
Cython           : None
pytest           : None
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : 2.11.2
IPython          : 7.16.1
pandas_datareader: None
bs4              : None
bottleneck       : None
fastparquet      : None
gcsfs            : None
lxml.etree       : None
matplotlib       : None
numexpr          : None
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pytables         : None
pytest           : None
pyxlsb           : None
s3fs             : None
scipy            : None
sqlalchemy       : None
tables           : None
tabulate         : None
xarray           : None
xlrd             : None
xlwt             : None
xlsxwriter       : None
numba            : 0.50.1
```

Discussion:

Patch:
diff --git a/rle_array/array.py b/rle_array/array.py
index 915e6c2..896fe99 100644
--- a/rle_array/array.py
+++ b/rle_array/array.py
@@ -457,7 +457,7 @@ class RLEArray(ExtensionArray):
         if n == 0:
             return self.dtype.na_value
         else:
-            return np.dot(data, lengths) / self.dtype._dtype.type(n)
+            return np.dot(data, lengths) / np.float64(n)
 
     def median(
         self, skipna: bool = True, axis: Optional[int] = 0, out: Any = None


Test patch:
diff --git a/test-requirements.txt b/test-requirements.txt
index 43ac36b..9032352 100644
--- a/test-requirements.txt
+++ b/test-requirements.txt
@@ -1,8 +1,8 @@
 asv
 black==19.10b0
 flake8-mutable==1.2.0
-flake8==3.8.3
-isort==5.0.9
+flake8==3.8.2
+isort==4.3.21
 mypy
 pytest
 pytest-cov
diff --git a/tests/test_regressions.py b/tests/test_regressions.py
index 7027b2f..e55e9ee 100644
--- a/tests/test_regressions.py
+++ b/tests/test_regressions.py
@@ -15,3 +15,9 @@ def test_object_isna():
     actual = array.isna()
     expected = np.asarray([False, True])
     npt.assert_equal(actual, expected)
+
+
+def test_mean_divisor_overflow():
+    # https://github.com/JDASoftwareGroup/rle-array/issues/22
+    array = RLEArray._from_sequence([1] * 256, dtype=np.uint8)
+    assert array.mean() == 1