davlee1972 commented on issue #13111:
URL: https://github.com/apache/arrow/issues/13111#issuecomment-1644853602

   I got a temporary solution which modifies pyarrow.compute.strptime(), but I 
can't seem to figure out how to adjust a timestamp array by +/- number of 
hours.. tried using pyarrow.compute.add()
   
   Here's the almost completed code..
   
   ```
   import pyarrow.compute as pc
   
   old_strpttime = pc.strptime
   
   def new_strptime(*args, **kwargs):
       if 'format' in kwargs and '%p' in kwargs['format'] and '%I' in 
kwargs['format']:
           is_am = pc.count_substring(_ts, 'AM')
           kwargs['format'] = kwargs['format'].replace('%p','')
           strings = pc.replace_substring(pc.replace_substring(args[0], 'AM', 
''), 'PM', '')
           new_timestamps = old_strpttime(strings, **kwargs)
           hour_adjustments = pc.case_when(pc.make_struct(
               pc.and_(is_am.cast("bool"), pc.equal(pc.hour(new_timestamps), 
12)),
               pc.and_(pc.invert(is_am.cast("bool")), 
pc.equal(pc.hour(new_timestamps), 12)),
               is_am.cast("bool")
               ),
               -12,
               0,
               0,
               12
           )
           return {'original timestamps': args[0], 'without am|pm': 
new_timestamps, 'hours adjusted': hour_to adjust}
       else:
           return old_strpttime(*args, **kwargs)
   
   pc.strptime = new_strptime
    
   ```
   
   and the output with and without AM/PM hour additions..
   
   ```
   >>> _ts = ["9/03/2023 00:35", "9/03/2023 12:35", "9/03/2023 6:35", 
"9/03/2023 18:35"]
   >>> _format = "%d/%m/%Y %H:%M"
   >>>
   >>> pc.strptime(_ts, format= _format, unit='s')
   <pyarrow.lib.TimestampArray object at 0x000001D0D0E48880>
   [
     2023-03-09 00:35:00,
     2023-03-09 12:35:00,
     2023-03-09 06:35:00,
     2023-03-09 18:35:00
   ]
   >>> _ts = ["9/03/2023 12:35 AM", "9/03/2023 12:35 PM", "9/03/2023 6:35 AM", 
"9/03/2023 6:35 PM"]
   >>> _format = "%d/%m/%Y %I:%M %p"
   >>>
   >>> pc.strptime(_ts, format= _format, unit='s')
   {'original timestamps': ['9/03/2023 12:35 AM', '9/03/2023 12:35 PM', 
'9/03/2023 6:35 AM', '9/03/2023 6:35 PM'], 'without am|pm': 
<pyarrow.lib.TimestampArray object at 0x000001D0D0E48D00>
   [
     2023-03-09 12:35:00,
     2023-03-09 12:35:00,
     2023-03-09 06:35:00,
     2023-03-09 06:35:00
   ], 'hours to adjust': <pyarrow.lib.Int64Array object at 0x000001D0D0E48E20>
   [
     -12,
     0,
     0,
     12
   ]}
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to