> For the complete documentation index, see [llms.txt](https://zeliang-yao.gitbook.io/my-note-zeliang-yao/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zeliang-yao.gitbook.io/my-note-zeliang-yao/useful/pandas-timeseries/to-date-time.md).

# Function\_to\_date\_time

In \[1]:

```python
import pandas as pd
dates = ['2017-01-05', 'Jan 5, 2017', '01/05/2017', '2017.01.05', '2017/01/05','20170105']
pd.to_datetime(dates)
```

Out\[1]:

```python
DatetimeIndex(['2017-01-05', '2017-01-05', '2017-01-05', '2017-01-05',
               '2017-01-05', '2017-01-05'],
              dtype='datetime64[ns]', freq=None)
```

In \[2]:

```python
dt = ['2017-01-05 2:30:00 PM', 'Jan 5, 2017 14:30:00', '01/05/2016', '2017.01.05', '2017/01/05','20170105']
pd.to_datetime(dt)
```

Out\[2]:

```python
DatetimeIndex(['2017-01-05 14:30:00', '2017-01-05 14:30:00',
               '2016-01-05 00:00:00', '2017-01-05 00:00:00',
               '2017-01-05 00:00:00', '2017-01-05 00:00:00'],
              dtype='datetime64[ns]', freq=None)
```

## European style dates with day first <a href="#european-style-dates-with-day-first" id="european-style-dates-with-day-first"></a>

* US: mm/dd/yy
* Eur: dd/mm/yy

In \[6]:

```python
pd.to_datetime('01-07-2019')
```

Out\[6]:

```python
Timestamp('2019-01-07 00:00:00')
```

In \[8]:

```python
pd.to_datetime('01-07-2019', dayfirst=True)
```

Out\[8]:

```python
Timestamp('2019-07-01 00:00:00')
```

In \[9]:

```python
pd.to_datetime('2019$01$07', format='%Y$%m$%d')
```

Out\[9]:

```python
Timestamp('2019-01-07 00:00:00')
```

In \[10]:

```python
pd.to_datetime('2019#01#07', format='%Y#%m#%d')
```

Out\[10]:

```python
Timestamp('2019-01-07 00:00:00')
```

In \[13]:

```python
# Leave the invalid values (‘abc’)
pd.to_datetime(['2017-01-05', 'Jan 6, 2017', 'abc'], errors='ignore')
```

Out\[13]:

```python
Index(['2017-01-05', 'Jan 6, 2017', 'abc'], dtype='object')
```

In \[14]:

```python
# Assign NaT type for invalid values
pd.to_datetime(['2017-01-05', 'Jan 6, 2017', 'abc'], errors='coerce')
```

Out\[14]:

```python
DatetimeIndex(['2017-01-05', '2017-01-06', 'NaT'], dtype='datetime64[ns]', freq=None)
```

### **Epoch or Unix time means number of seconds that have passed since Jan 1, 1970 00:00:00 UTC time** <a href="#epoch-or-unix-time-means-number-of-seconds-that-have-passed-since-jan-1-1970-00-00-00-utc-time" id="epoch-or-unix-time-means-number-of-seconds-that-have-passed-since-jan-1-1970-00-00-00-utc-time"></a>

In \[21]:

```python
# Search Online
current_epoch = 1563979818
pd.to_datetime(current_epoch, unit='s')
```

Out\[21]:

```python
Timestamp('2019-07-24 14:50:18')
```

In \[22]:

```python
pd.to_datetime(current_epoch*1000, unit='ms')
```

Out\[22]:

```python
Timestamp('2019-07-24 14:50:18')
```

In \[25]:

```python
# change format from tuple to list, if not can't convert it back
t = pd.to_datetime([current_epoch], unit='s')
t
```

Out\[25]:

```python
DatetimeIndex(['2019-07-24 14:50:18'], dtype='datetime64[ns]', freq=None)
```

Out\[26]:

```python
array([1563979818000000000], dtype=int64)
```
