# Buggy code
def construct_1d_arraylike_from_scalar(
    value: Scalar, length: int, dtype: Optional[DtypeObj]
) -> ArrayLike:
    """
    create a np.ndarray / pandas type of specified shape and dtype
    filled with values

    Parameters
    ----------
    value : scalar value
    length : int
    dtype : pandas_dtype or np.dtype

    Returns
    -------
    np.ndarray / pandas type of length, filled with value

    """

    if dtype is None:
        dtype, value = infer_dtype_from_scalar(value, pandas_dtype=True)

    if is_extension_array_dtype(dtype):
        cls = dtype.construct_array_type()
        subarr = cls._from_sequence([value] * length, dtype=dtype)

    else:
        if length and is_integer_dtype(dtype) and isna(value):
            # coerce if we have nan for an integer dtype
            dtype = np.dtype("float64")
        elif isinstance(dtype, np.dtype) and dtype.kind in ("U", "S"):
            # we need to coerce to object dtype to avoid
            # to allow numpy to take our string as a scalar value
            dtype = np.dtype("object")
            if not isna(value):
                value = ensure_str(value)
        elif dtype.kind in ["M", "m"] and is_valid_nat_for_dtype(value, dtype):
            # GH36541: can't fill array directly with pd.NaT
            # > np.empty(10, dtype="datetime64[64]").fill(pd.NaT)
            # ValueError: cannot convert float NaN to integer
            value = dtype.type("NaT", "ns")

        subarr = np.empty(length, dtype=dtype)
        subarr.fill(value)

    return subarr


# Traceback (AssertionError)
In [31]: import pandas as pd
...: import pandas._testing as tm
...:
...: td = pd.Timedelta(nanoseconds=500)
...: ser = pd.Series({"a": td})
...: expected = pd.Series(td, index=["a"], dtype="timedelta64[ns]")
...:
...: tm.assert_series_equal(ser, expected)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-31-a9c6a6312101> in <module>
6 expected = pd.Series(td, index=["a"], dtype="timedelta64[ns]")
7
----> 8 tm.assert_series_equal(ser, expected)

[... skipping hidden 1 frame]

~/repos/pandas/pandas/_testing.py in assert_extension_array_equal(left, right, check_dtype, index_values, check_less_precise, check_exact, rtol, atol)
1243         # Avoid slow object-dtype comparisons
1244         # np.asarray for case where we have a np.MaskedArray
-> 1245         assert_numpy_array_equal(
1246             np.asarray(left.asi8), np.asarray(right.asi8), index_values=index_values
1247         )

[... skipping hidden 1 frame]

~/repos/pandas/pandas/_testing.py in _raise(left, right, err_msg)
1155             diff = diff * 100.0 / left.size
1156             msg = f"{obj} values are different ({np.round(diff, 5)} %)"
-> 1157             raise_assert_detail(obj, msg, left, right, index_values=index_values)
1158
1159         raise AssertionError(err_msg)

~/repos/pandas/pandas/_testing.py in raise_assert_detail(obj, message, left, right, diff, index_values)
1085         msg += f"\n[diff]: {diff}"
1086
-> 1087     raise AssertionError(msg)
1088
1089

AssertionError: numpy array are different

numpy array values are different (100.0 %)
[index]: [a]
[left]:  [500]
[right]: [0]

# Fixed code
def construct_1d_arraylike_from_scalar(
    value: Scalar, length: int, dtype: Optional[DtypeObj]
) -> ArrayLike:
    """
    create a np.ndarray / pandas type of specified shape and dtype
    filled with values

    Parameters
    ----------
    value : scalar value
    length : int
    dtype : pandas_dtype or np.dtype

    Returns
    -------
    np.ndarray / pandas type of length, filled with value

    """

    if dtype is None:
        dtype, value = infer_dtype_from_scalar(value, pandas_dtype=True)

    if is_extension_array_dtype(dtype):
        cls = dtype.construct_array_type()
        subarr = cls._from_sequence([value] * length, dtype=dtype)

    else:
        if length and is_integer_dtype(dtype) and isna(value):
            # coerce if we have nan for an integer dtype
            dtype = np.dtype("float64")
        elif isinstance(dtype, np.dtype) and dtype.kind in ("U", "S"):
            # we need to coerce to object dtype to avoid
            # to allow numpy to take our string as a scalar value
            dtype = np.dtype("object")
            if not isna(value):
                value = ensure_str(value)
        elif dtype.kind in ["M", "m"]:
            value = maybe_unbox_datetimelike(value, dtype)

        subarr = np.empty(length, dtype=dtype)
        subarr.fill(value)

    return subarr