# Buggy code
def _period_array_cmp(cls, op):
    """
    Wrap comparison operations to convert Period-like to PeriodDtype
    """
    opname = "__{name}__".format(name=op.__name__)
    nat_result = True if opname == "__ne__" else False

    def wrapper(self, other):
        op = getattr(self.asi8, opname)
        # We want to eventually defer to the Series or PeriodIndex (which will
        # return here with an unboxed PeriodArray). But before we do that,
        # we do a bit of validation on type (Period) and freq, so that our
        # error messages are sensible
        not_implemented = isinstance(other, (ABCSeries, ABCIndexClass))
        if not_implemented:
            other = other._values

        if isinstance(other, Period):
            self._check_compatible_with(other)

            result = op(other.ordinal)
        elif isinstance(other, cls):
            self._check_compatible_with(other)

            if not_implemented:
                return NotImplemented
            result = op(other.asi8)

            mask = self._isnan | other._isnan
            if mask.any():
                result[mask] = nat_result

            return result
        elif other is NaT:
            result = np.empty(len(self.asi8), dtype=bool)
            result.fill(nat_result)
        else:
            other = Period(other, freq=self.freq)
            result = op(other.ordinal)

        if self._hasnans:
            result[self._isnan] = nat_result

        return result

    return compat.set_function_name(wrapper, opname, cls)


# Traceback (ValueError)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-8a2a887e6efd> in <module>
----> 1 idx <= idx[[0]]

~/sandbox/pandas/pandas/core/indexes/datetimes.py in wrapper(self, other)
90
91     def wrapper(self, other):
---> 92         result = getattr(DatetimeArrayMixin, opname)(self, other)
93         if is_bool_dtype(result):
94             return result

~/sandbox/pandas/pandas/core/arrays/datetimes.py in wrapper(self, other)
133             else:
134                 self._assert_tzawareness_compat(other)
--> 135                 result = meth(self, np.asarray(other))
136
137             result = com.values_from_object(result)

~/sandbox/pandas/pandas/core/arrays/datetimelike.py in cmp_method(self, other)
51         if isinstance(other, (np.ndarray, ABCIndexClass, ABCSeries)):
52             if other.ndim > 0 and len(self) != len(other):
---> 53                 raise ValueError('Lengths must match to compare')
54
55         if needs_i8_conversion(self) and needs_i8_conversion(other):

ValueError: Lengths must match to compare

# Fixed code
def _period_array_cmp(cls, op):
    """
    Wrap comparison operations to convert Period-like to PeriodDtype
    """
    opname = "__{name}__".format(name=op.__name__)
    nat_result = True if opname == "__ne__" else False

    def wrapper(self, other):
        op = getattr(self.asi8, opname)
        # We want to eventually defer to the Series or PeriodIndex (which will
        # return here with an unboxed PeriodArray). But before we do that,
        # we do a bit of validation on type (Period) and freq, so that our
        # error messages are sensible
        if is_list_like(other) and len(other) != len(self):
            raise ValueError("Lengths must match")

        not_implemented = isinstance(other, (ABCSeries, ABCIndexClass))
        if not_implemented:
            other = other._values

        if isinstance(other, Period):
            self._check_compatible_with(other)

            result = op(other.ordinal)
        elif isinstance(other, cls):
            self._check_compatible_with(other)

            if not_implemented:
                return NotImplemented
            result = op(other.asi8)

            mask = self._isnan | other._isnan
            if mask.any():
                result[mask] = nat_result

            return result
        elif other is NaT:
            result = np.empty(len(self.asi8), dtype=bool)
            result.fill(nat_result)
        else:
            other = Period(other, freq=self.freq)
            result = op(other.ordinal)

        if self._hasnans:
            result[self._isnan] = nat_result

        return result

    return compat.set_function_name(wrapper, opname, cls)