<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def trim_data(x):
 """ Trim leading and trailing NaNs from dataset This is done by browsing the array from each end and store the index of the first non-NaN in each case, the return the appropriate slice of the array """

    # Find indices for first and last valid data
    first = 0
    while np.isnan(x[first]):
        first += 1
    last = len(x)
    while np.isnan(x[last - 1]):
        last -= 1
    return x[first:last]