Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extract only the date from a timestamp?

I have dataframe just like that:

df = pd.DataFrame({
    'timestamp': ['2017-06-04 07:59:42', '2017-06-04 07:59:42',
                  '2017-06-04 07:59:42', '2017-06-04 07:59:42',
                  '2017-06-04 07:59:42'],
    'municipality_id': [9, 8, 4, 0, 7],
    'usage': [454, 556, 1090, 204, 718],
    'total_capacity': [1332, 2947, 3893, 2813, 2019]
})
timestamp municipality_id usage total_capacity
2017-06-04 07:59:42 9 454 1332
2017-06-04 07:59:42 8 556 2947
2017-06-04 07:59:42 4 1090 3893
2017-06-04 07:59:42 0 204 2813
2017-06-04 07:59:42 7 718 2019

So how can I get the first ten characters of every row of first column like:

0    2017-06-04
1    2017-06-04
2    2017-06-04
3    2017-06-04
4    2017-06-04
like image 958
Muhammed Erem Avatar asked Nov 07 '25 01:11

Muhammed Erem


1 Answers

From your csv file, there are multiple methods to extract the date

Method 1: parse_dates from read_csv:

df = pd.read_csv('data.csv', parse_dates=['timestamp'])
df['date'] = df['timestamp'].dt.date

Method 2: to_datetime:

df = pd.read_csv('data.csv')
df['date'] = pd.to_datetime(df['timestamp']).dt.date

Method 3: str[]

df = pd.read_csv('data.csv')
df['date'] = df['timestamp'].str[:10]

The output of each method is:

>>> df['date']
1        2017-06-04
2        2017-06-04
3        2017-06-04
4        2017-06-04
            ...    
13065    2017-08-19
13066    2017-08-19
13067    2017-08-19
13068    2017-08-19
13069    2017-08-19
Name: date, Length: 13070, dtype: object
like image 186
Corralien Avatar answered Nov 09 '25 16:11

Corralien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!