Repository: astropy/astropy

Issue:
Bayesian Blocks type error
Hi,
I'm trying to use the Bayesian Block routine but when I use the 'events' method, I always get this error message: 

 ```
   sgra_edges = bayesian_blocks(sgra_time, sgra_counts, sgra_counts_err, fitness='events', p0=0.05)
  File "/home/elie/anaconda2/lib/python2.7/site-packages/astropy/stats/bayesian_blocks.py", line 157, in bayesian_blocks
    return fitfunc.fit(t, x, sigma)
  File "/home/elie/anaconda2/lib/python2.7/site-packages/astropy/stats/bayesian_blocks.py", line 322, in fit
    t, x, sigma = self.validate_input(t, x, sigma)
  File "/home/elie/anaconda2/lib/python2.7/site-packages/astropy/stats/bayesian_blocks.py", line 438, in validate_input
    t, x, sigma = super(Events, self).validate_input(t, x, sigma)
  File "/home/elie/anaconda2/lib/python2.7/site-packages/astropy/stats/bayesian_blocks.py", line 252, in validate_input
    x += np.zeros_like(t)
TypeError: Cannot cast ufunc add output from dtype('float64') to dtype('>i4') with casting rule 'same_kind'
```

I'm using python 2.7 on Ubuntu and have astropy up-to-date.

Discussion:

Patch:
diff --git a/CHANGES.rst b/CHANGES.rst
index 8f95bd98c6..e66b543467 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -700,6 +700,9 @@ astropy.stats
 
 - Fixed a bug where ``bayesian_blocks`` returned a single edge. [#8560]
 
+- Fixed input data type validation for ``bayesian_blocks`` to work int
+  arrays. [#9513]
+
 astropy.table
 ^^^^^^^^^^^^^
 
diff --git a/astropy/stats/bayesian_blocks.py b/astropy/stats/bayesian_blocks.py
index d4f5406552..bd26e67596 100644
--- a/astropy/stats/bayesian_blocks.py
+++ b/astropy/stats/bayesian_blocks.py
@@ -213,10 +213,6 @@ class FitnessFunc:
         """
         # validate array input
         t = np.asarray(t, dtype=float)
-        if x is not None:
-            x = np.asarray(x)
-        if sigma is not None:
-            sigma = np.asarray(sigma)
 
         # find unique values of t
         t = np.array(t)
@@ -242,7 +238,8 @@ class FitnessFunc:
         # if x is specified, then we need to simultaneously sort t and x
         else:
             # TODO: allow broadcasted x?
-            x = np.asarray(x)
+            x = np.asarray(x, dtype=float)
+
             if x.shape not in [(), (1,), (t.size,)]:
                 raise ValueError("x does not match shape of t")
             x += np.zeros_like(t)
@@ -257,7 +254,7 @@ class FitnessFunc:
         if sigma is None:
             sigma = 1
         else:
-            sigma = np.asarray(sigma)
+            sigma = np.asarray(sigma, dtype=float)
             if sigma.shape not in [(), (1,), (t.size,)]:
                 raise ValueError('sigma does not match the shape of x')
 


Test patch:
diff --git a/astropy/stats/tests/test_bayesian_blocks.py b/astropy/stats/tests/test_bayesian_blocks.py
index 5b7b4bf754..f79e14e5c4 100644
--- a/astropy/stats/tests/test_bayesian_blocks.py
+++ b/astropy/stats/tests/test_bayesian_blocks.py
@@ -27,7 +27,8 @@ def test_duplicate_events(rseed=0):
     t = rng.rand(100)
     t[80:] = t[:20]
 
-    x = np.ones_like(t)
+    # Using int array as a regression test for gh-6877
+    x = np.ones(t.shape, dtype=int)
     x[:20] += 1
 
     with pytest.warns(AstropyUserWarning,