1

I have sliced the pandas dataframe.

end_date = df[-1:]['end'] 

type(end_date) 
Out[4]: pandas.core.series.Series 

end_date 
Out[3]: 
48173   2017-09-20 04:47:59 
Name: end, dtype: datetime64[ns] 
  1. How to get rid of end_date's index value 48173 and get only 2017-09-20 04:47:59 string? I have to call REST API with 2017-09-20 04:47:59 as a parameter, so I have to get string from pandas datetime64 series.
  2. How to get rid of end_date's index value 48173 and get only datetime object [something like datetime.datetime.strptime('2017-09-20 04:47:59', '%Y-%m-%d %H:%M:%S')]. I need it because, later I will have to check if '2017-09-20 04:47:59' < datetime.datetime(2017,1,9)

I need to convert just a single cell value, not a whole column. How to do these conversions?

3kt
  • 2,543
  • 1
  • 17
  • 29
user3225309
  • 1,183
  • 3
  • 15
  • 31
  • Please separate your code from the execution in the interpreter, as it is unclear. – Ardit Sep 22 '17 at 08:51
  • 3
    `df[-1:]['a'].item()` or `df[-1:]['a'].values[0]`? – Zero Sep 22 '17 at 08:52
  • This works. Thanks. – user3225309 Sep 24 '17 at 11:15
  • I am confused. `import pandas as pd df = pd.DataFrame(pd.date_range('2017-01-01', periods=5, freq='T'), columns=['end']) df[-1:]['end'] 4 2017-01-01 00:04:00 Name: end, dtype: datetime64[ns] df['end'].iloc[-1] Timestamp('2017-01-01 00:04:00')` The line before last line returns index and datetime64 but last line returns Timestamp. – user3225309 Sep 26 '17 at 06:33

2 Answers2

5

It seems you need:

import pandas as pd
data = ['2017-09-20 04:47:59','2017-10-20 04:47:59','2017-09-30 04:47:59']
df = pd.DataFrame(data,columns=['end'])
df['end'] = pd.to_datetime(df['end'])
df

df will be:

    end
0   2017-09-20 04:47:59
1   2017-10-20 04:47:59
2   2017-09-30 04:47:59

After that you can use below code to get rid of index and use as 'Timestamp' object:

end_date = df['end'].iloc[-1] #get last row of column end
print(type(end_date)) # pandas.tslib.Timestamp
end_date_str = end_date.strftime('%Y-%m-%d %H:%M:%S') #convert to str
print(end_date_str) # '2017-09-30 04:47:59'
print(end_date < datetime.datetime(2017,1,9)) #False
Tiny.D
  • 6,466
  • 2
  • 15
  • 20
  • 1
    @user3225309 glad it helps, if it is the right solution, please mark as an accept answer :) – Tiny.D Sep 26 '17 at 06:12
3

Simply cast the result to a string, and recover it using .values[0]:

In [38]: end_date
Out[38]:
48173   2017-09-20 04:47:59
Name: end, dtype: datetime64[ns]

In [39]: end_date.astype(str).values[0]
Out[39]: '2017-09-20 04:47:59'

If you want a datetime object, you have to convert it to a timestamp, and then back to a datetime object:

In [42]: end_date.values[0].item()
Out[42]: 1505882879000000000

In [43]: datetime.fromtimestamp(end_date.values[0].item()/10**9)
Out[43]: datetime.datetime(2017, 9, 20, 6, 47, 59)

Otherwise, you can strptime the string recovered in step 1:

In [48]: datetime.datetime.strptime(end_date.astype(str).values[0], '%Y-%m-%d %H:%M:%S')
Out[48]: datetime.datetime(2017, 9, 20, 4, 47, 59)

You may wonder why there is a 2 hours difference between the results. This is because the datetime.datetime.fromtimestamp takes my timezone into account (currently CEST, which is UTC+2). On the other hand, parsing a string to a datetime object doesn't yield any timezone information, srtptime naively parses the timestamp without regards for the timezone, which leads to a 2 hours discrepancy.

3kt
  • 2,543
  • 1
  • 17
  • 29
  • end_date.astype(str).values[0] works. Thanks. But np.datetime64(datetime.datetime.utcnow()).astype(datetime) Traceback (most recent call last): File "C:\Users\...\AppData\Local\Programs\Python\Python35\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 1, in np.datetime64(datetime.datetime.utcnow()).astype(datetime) TypeError: data type not understood – user3225309 Sep 24 '17 at 11:26
  • datetime.datetime.strptime(end_date.astype(str).values[0], '%Y-%m-%d %H:%M:%S') works as well. – user3225309 Sep 24 '17 at 11:30
  • @user3225309 what are you trying to achieve in your first comment ? Convert directly to datetime ? You can't cast directly through pandas, abd have to refer to the answer I linked. Moreover, if it solves your problem, feel free to "accept the answer" – 3kt Sep 25 '17 at 07:43
  • On link you have provided I found the following: np.datetime64(datetime.datetime.utcnow()).astype(datetime) which doesn't work in my case. – user3225309 Sep 26 '17 at 06:08
  • @user3225309 sorry, I didn't notice you were using a `datetime64[ns]` and not a `datetime64`. I updated my answer to match that. – 3kt Sep 26 '17 at 08:41