> ## Documentation Index
> Fetch the complete documentation index at: https://knowledge.cloudquant.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Extracting Parts of a Date or Time from a Timestamp

> Efficiently extract date and time components from timestamp strings in Python DataFrames using string slicing and pandas methods.

# Extracting parts of a date or time from a timestamp

When working with timestamped market data, you often need to extract specific components like the date, time, or hour. Rather than using slower row-by-row loops, you can use vectorized operations for much better performance.

## String slicing with lambda functions

The primary approach uses `apply()` with lambda functions to extract substrings from timestamp data:

```python theme={null}
df = liberator.get_dataframe(
    liberator.query(
        name='minute_bars',
        symbols='SPY',
        as_of='2024-07-26',
        back_to='2024-07-25'
    )
)

df['Date'] = df.apply(lambda row: str(row.timestamp)[0:10], axis=1)
df['Time'] = df.apply(lambda row: str(row.timestamp)[11:16], axis=1)
df['Hour'] = df.apply(lambda row: str(row.timestamp)[11:13], axis=1)
df['DateTime'] = df.apply(lambda row: str(row.timestamp)[0:16], axis=1)
df['DateHour'] = df.apply(lambda row: str(row.timestamp)[0:13], axis=1)

df[['timestamp', 'Date', 'Time', 'Hour', 'DateTime', 'DateHour']]
```

This creates new columns by slicing the timestamp string at specific positions:

| Column     | Slice     | Example Output     |
| ---------- | --------- | ------------------ |
| `Date`     | `[0:10]`  | `2024-07-26`       |
| `Time`     | `[11:16]` | `14:30`            |
| `Hour`     | `[11:13]` | `14`               |
| `DateTime` | `[0:16]`  | `2024-07-26 14:30` |
| `DateHour` | `[0:13]`  | `2024-07-26 14`    |

## Alternative: FLOOR method using Pandas DateTime

You can also use pandas built-in datetime methods:

```python theme={null}
df['seconds'] = df['timestamp'].dt.floor('s')

df['Date'] = pd.to_datetime(df["timestamp"]).dt.date
```

<Tip>
  String slicing with lambda functions provides fast processing for extracting timestamp components, making it preferable to manual loops for DataFrame operations.
</Tip>
