# Buggy code
def _yt_array_hdf5_attr(fh, attr, val):
    r"""Save a YTArray or YTQuantity as an hdf5 attribute.

    Save an hdf5 attribute.  If it has units, save an
    additional attribute with the units.

    Parameters
    ----------
    fh : an open hdf5 file, group, or dataset
        The hdf5 file, group, or dataset to which the
        attribute will be written.
    attr : str
        The name of the attribute to be saved.
    val : anything
        The value to be saved.

    """

    if val is None:
        val = "None"
    if hasattr(val, "units"):
        fh.attrs["%s_units" % attr] = str(val.units)
    # The following is a crappy workaround for getting
    # Unicode strings into HDF5 attributes in Python 3
    if iterable(val):
        val = np.array(val)
        if val.dtype.kind == "U":
            val = val.astype("|S")
    fh.attrs[str(attr)] = val


# Traceback (TypeError)
#!python

-----------------------------------------------------------------
yt : [INFO     ] 2017-03-03 15:06:16,097 Calculating time from 1.000e+00 to be 4.376e+17 seconds
yt : [INFO     ] 2017-03-03 15:06:16,098 Assuming length units are in kpc/h (comoving)
yt : [INFO     ] 2017-03-03 15:06:16,127 Parameters: current_time              = 4.37587753506e+17 s
yt : [INFO     ] 2017-03-03 15:06:16,127 Parameters: domain_dimensions         = [2 2 2]
yt : [INFO     ] 2017-03-03 15:06:16,128 Parameters: domain_left_edge          = [ 0.  0.  0.]
yt : [INFO     ] 2017-03-03 15:06:16,128 Parameters: domain_right_edge         = [ 5000.  5000.  5000.]
yt : [INFO     ] 2017-03-03 15:06:16,129 Parameters: cosmological_simulation   = 1
yt : [INFO     ] 2017-03-03 15:06:16,129 Parameters: current_redshift          = -2.22044604925e-16
yt : [INFO     ] 2017-03-03 15:06:16,129 Parameters: omega_lambda              = 0.73
yt : [INFO     ] 2017-03-03 15:06:16,129 Parameters: omega_matter              = 0.27
yt : [INFO     ] 2017-03-03 15:06:16,129 Parameters: hubble_constant           = 0.7
yt : [INFO     ] 2017-03-03 15:06:16,134 Allocating for 1.370e+06 particles (index particle type 'all')
yt : [INFO     ] 2017-03-03 15:06:16,388 Identified 9.178e+04 octs
yt : [INFO     ] 2017-03-03 15:06:44,399 Max Value is 1.00458e-25 at 2255.8593750000000000 2568.3593750000000000 2802.7343750000000000
yt : [INFO     ] 2017-03-03 15:06:45,874 Saving field data to yt dataset: Gadget3-snap-format2_sphere.h5.
yt : [INFO     ] 2017-03-03 15:06:47,395 Saving field data to yt dataset: Gadget3-snap-format2_cut_region.h5.
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-5b545a06497d> in <module>()
4 sp.save_as_dataset(fields=["temperature"])
5 kt = ds.cut_region(sp,['(obj["kT"] < 75) &amp; (obj["kT"] > 2)'])
----> 6 fn = kt.save_as_dataset(fields=["density"])

/usr/local/lib/python2.7/dist-packages/yt/data_objects/data_containers.pyc in save_as_dataset(self, filename, fields)
591         extra_attrs["dimensionality"] = self._dimensionality
592         save_as_dataset(self.ds, filename, data, field_types=ftypes,
--> 593                         extra_attrs=extra_attrs)
594
595         return filename

/usr/local/lib/python2.7/dist-packages/yt/frontends/ytdata/utilities.pyc in save_as_dataset(ds, filename, data, field_types, extra_attrs)
116     for attr in extra_attrs:
117         my_val = extra_attrs[attr]
--> 118         _yt_array_hdf5_attr(fh, attr, my_val)
119     if "data_type" not in extra_attrs:
120         fh.attrs["data_type"] = "yt_array_data"

/usr/local/lib/python2.7/dist-packages/yt/frontends/ytdata/utilities.pyc in _yt_array_hdf5_attr(fh, attr, val)
231         if val.dtype.kind == 'U':
232             val = val.astype('|S')
--> 233     fh.attrs[str(attr)] = val

h5py/_objects.pyx in h5py._objects.with_phil.wrapper (/tmp/pip-4rPeHA-build/h5py/_objects.c:2684)()

h5py/_objects.pyx in h5py._objects.with_phil.wrapper (/tmp/pip-4rPeHA-build/h5py/_objects.c:2642)()

/usr/local/lib/python2.7/dist-packages/h5py/_hl/attrs.pyc in __setitem__(self, name, value)
91         use the methods create() and modify().
92         """
---> 93         self.create(name, data=value, dtype=base.guess_dtype(value))
94
95     @with_phil

/usr/local/lib/python2.7/dist-packages/h5py/_hl/attrs.pyc in create(self, name, data, shape, dtype)
167             # Make HDF5 datatype and dataspace for the H5A calls
168             if use_htype is None:
--> 169                 htype = h5t.py_create(original_dtype, logical=True)
170                 htype2 = h5t.py_create(original_dtype)  # Must be bit-for-bit representation rather than logical
171             else:

h5py/h5t.pyx in h5py.h5t.py_create (/tmp/pip-4rPeHA-build/h5py/h5t.c:16078)()

h5py/h5t.pyx in h5py.h5t.py_create (/tmp/pip-4rPeHA-build/h5py/h5t.c:15912)()

h5py/h5t.pyx in h5py.h5t.py_create (/tmp/pip-4rPeHA-build/h5py/h5t.c:15813)()

TypeError: Object dtype dtype('O') has no native HDF5 equivalent

# Fixed code
def _yt_array_hdf5_attr(fh, attr, val):
    r"""Save a YTArray or YTQuantity as an hdf5 attribute.

    Save an hdf5 attribute.  If it has units, save an
    additional attribute with the units.

    Parameters
    ----------
    fh : an open hdf5 file, group, or dataset
        The hdf5 file, group, or dataset to which the
        attribute will be written.
    attr : str
        The name of the attribute to be saved.
    val : anything
        The value to be saved.

    """

    if val is None:
        val = "None"
    if hasattr(val, "units"):
        fh.attrs["%s_units" % attr] = str(val.units)
    # The following is a crappy workaround for getting
    # Unicode strings into HDF5 attributes in Python 3
    if iterable(val):
        val = np.array(val)
        if val.dtype.kind == "U":
            val = val.astype("|S")
    try:
        fh.attrs[str(attr)] = val
    # This is raised if no HDF5 equivalent exists.
    # In that case, save its string representation.
    except TypeError:
        fh.attrs[str(attr)] = str(val)