Repository: pydata/xarray

Issue:
xarray .count() gives -1 when it should be 1? 
Data array `da` I'm working with looks like: 
```
<xarray.DataArray 'T' (time: 365, latitude: 256, longitude: 512)>
[47841280 values with dtype=float32]
Coordinates:
  * time         (time) datetime64[ns] 1979-01-01T09:00:00 ... 1979-12-31T09:00:00
  * latitude     (latitude) float32 89.462944 88.766945 ... -89.462944
  * longitude    (longitude) float32 0.0 0.703125 ... 358.59375 359.29688
```

Now `int(da.time.count())` gives 365 which is correct. However when I select a time coordinate `da2 = da.isel(time=0)` the resulting array has one time coordinate:

```
<xarray.DataArray 'T' (latitude: 256, longitude: 512)>
[131072 values with dtype=float32]
Coordinates:
    time         datetime64[ns] 1979-01-01T09:00:00
  * latitude     (latitude) float32 89.462944 88.766945 ... -89.462944
  * longitude    (longitude) float32 0.0 0.703125 ... 358.59375 359.29688
```

and `int(da2.time.count())` gives me -1 instead of 1. I wasn't sure if this was a bug or I wasn't selecting the coordinate properly - if this is the case how should I call the time coordinate to count it correctly? 

Discussion:

Patch:
diff --git a/doc/whats-new.rst b/doc/whats-new.rst
index 15ef2b00..e9bdc710 100644
--- a/doc/whats-new.rst
+++ b/doc/whats-new.rst
@@ -26,6 +26,8 @@ Bug fixes
 
 - indexing with an empty list creates an object with zero-length axis (:issue:`2882`)
   By `Mayeul d'Avezac <https://github.com/mdavezac>`_.
+- Return correct count for scalar datetime64 arrays (:issue:`2770`)
+  By `Dan Nowacki <https://github.com/dnowacki-usgs>`_.
 
 .. _whats-new.0.12.1:
 
diff --git a/xarray/core/duck_array_ops.py b/xarray/core/duck_array_ops.py
index b67a220e..b37e01cb 100644
--- a/xarray/core/duck_array_ops.py
+++ b/xarray/core/duck_array_ops.py
@@ -185,7 +185,7 @@ def array_notnull_equiv(arr1, arr2):
 def count(data, axis=None):
     """Count the number of non-NA in this array along the given axis or axes
     """
-    return np.sum(~isnull(data), axis=axis)
+    return np.sum(np.logical_not(isnull(data)), axis=axis)
 
 
 def where(condition, x, y):


Test patch:
diff --git a/xarray/tests/test_duck_array_ops.py b/xarray/tests/test_duck_array_ops.py
index 5d425f64..8dfe5bb4 100644
--- a/xarray/tests/test_duck_array_ops.py
+++ b/xarray/tests/test_duck_array_ops.py
@@ -86,6 +86,8 @@ class TestOps(object):
         expected = array([[1, 2, 3], [3, 2, 1]])
         assert_array_equal(expected, count(self.x, axis=-1))
 
+        assert 1 == count(np.datetime64('2000-01-01'))
+
     def test_where_type_promotion(self):
         result = where([True, False], [1, 2], ['a', 'b'])
         assert_array_equal(result, np.array([1, 'b'], dtype=object))